【问题标题】:My application crashes with this error - 'NSInvalidArgumentException'我的应用程序因此错误而崩溃 - 'NSInvalidArgumentException'
【发布时间】:2018-12-11 16:13:20
【问题描述】:

我创建了一个程序来检索 JSON 文件并实现了它

NSString *FilePath = [[NSBundle mainBundle]pathForResource:@"Message" ofType:@"json"];

NSData *data = [NSData dataWithContentsOfFile:FilePath];
NSError *error;
if(error){
    NSLog(@"Error and CAn't retrive data: %@", error.localizedDescription);

}else{
    NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"Your Json Dictionary values are %@", jsonDict);
    for(NSDictionary *valuesDictionary in jsonDict){
ShopCollectionObject *shopObject = [[ShopCollectionObject alloc]initWithID:[[valuesDictionary objectForKey:@"message_id"]integerValue] Name:[valuesDictionary objectForKey:@"product"] TimeAsPrice:[[valuesDictionary objectForKey:@"message_time"]integerValue] Avathar:[valuesDictionary objectForKey:@"item_image"] user:[valuesDictionary objectForKey:@"user_image"] Name_User:[valuesDictionary objectForKey:@"user_name"] LocationOfUser:[valuesDictionary objectForKey:@"locate_user"]];

但是我的应用程序在这里崩溃并出现上述错误

  [self.objectForArray addObject:shopObject];
    }

}

在下方更新了我的店铺收藏代码

Shopcollection object.h

#import <Foundation/Foundation.h>

@interface ShopCollectionObject : NSObject

-(instancetype) initWithID: (int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int) GivenTimeAsPrice Avathar:(NSString *) PhotoOfAvathar user:(NSString *)UserAvathar Name_User: (NSString *) UserNames LocationOfUser:(NSString *) USerLocationGiven;
@property (nonatomic) int msgID;
@property(nonatomic, strong)NSString* Name;
@property (nonatomic) int TimeAsPrice;
@property (nonatomic,strong) NSString* Avathar;
@property (nonatomic,strong) NSString* user;
@property (nonatomic,strong) NSString* Name_User;
@property(nonatomic,strong) NSString* LocationOfUser;
@end

Shopcollectionobject.m

#import "ShopCollectionObject.h"

@implementation ShopCollectionObject

-(instancetype)initWithID:(int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int)GivenTimeAsPrice Avathar:(NSString *)PhotoOfAvathar user:(NSString *)UserAvathar Name_User:(NSString *)UserNames LocationOfUser:(NSString *)USerLocationGiven{
    self = [super init];
    if(self){
     self.msgID = msgID;
    self.Name = Profile_name;
    self.TimeAsPrice = GivenTimeAsPrice;
    self.Avathar = PhotoOfAvathar;
    self.user = UserAvathar;
    self.Name_User = UserNames;
    self.LocationOfUser = USerLocationGiven;
}
return self;
}
@end

【问题讨论】:

  • 请检查 json 数据我认为一些数据转换问题
  • 看看你的 jsonDict 有没有可转换为 ShopCollectionObject 类的所有 dics。
  • 我检查了我的 JSON 数据,它工作正常...
  • 请添加您的 json 文件
  • 更新了我上面的 JSON

标签: ios objective-c json uicollectionview


【解决方案1】:

您可能没有初始化您的 objectForArray。所以当你尝试调用 addObject 时,它是在一个空对象上调用它。

【讨论】:

    【解决方案2】:

    ShopCollectionObject.h

    #import <Foundation/Foundation.h>
    
    @interface ShopCollectionObject : NSObject
    @property (nonatomic) int message_id;
    @property (strong, nonatomic) NSString *Name;
    @property (nonatomic) int TimeAsPrice;
    @property (strong, nonatomic) NSString *Avathar;//user,Name_User,LocationOfUser,message_id
    @property (strong, nonatomic) NSString *user;
    @property (strong, nonatomic) NSString *Name_User;
    @property (strong, nonatomic) NSString *LocationOfUser;
    -(instancetype) initWithID: (int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int) GivenTimeAsPrice Avathar:(NSString *) PhotoOfAvathar user:(NSString *)UserAvathar Name_User: (NSString *) UserNames LocationOfUser:(NSString *) USerLocationGiven;
    @property (nonatomic) int msgID;
    @end
    

    ShopCollectionObject.m

    #import "ShopCollectionObject.h"
    
    @implementation ShopCollectionObject
    -(instancetype)initWithID:(int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int)GivenTimeAsPrice Avathar:(NSString *)PhotoOfAvathar user:(NSString *)UserAvathar Name_User:(NSString *)UserNames LocationOfUser:(NSString *)USerLocationGiven{
        self = [super init];
        if(self){
            self.msgID = msgID;
            self.Name = Profile_name;
            self.TimeAsPrice = GivenTimeAsPrice;
            self.Avathar = PhotoOfAvathar;
            self.user = UserAvathar;
            self.Name_User = UserNames;
            self.LocationOfUser = USerLocationGiven;
        }
        return self;
    }
    @end
    

    ViewController.m

    #import "ViewController.h"
    #import "ShopCollectionObject.h"
    @interface ViewController ()
    {
        NSMutableArray *objectForArray;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        objectForArray = [[NSMutableArray alloc]init];
        NSString *FilePath = [[NSBundle mainBundle]pathForResource:@"Message" ofType:@"json"];
    
        NSData *data = [NSData dataWithContentsOfFile:FilePath];
        NSError *error;
        if(error){
            NSLog(@"Error and CAn't retrive data: %@", error.localizedDescription);
    
        }else{
            NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for(NSDictionary *valuesDictionary in jsonDict){
                ShopCollectionObject *shopObject = [[ShopCollectionObject alloc]initWithID:[[valuesDictionary objectForKey:@"message_id"]intValue] Name:[valuesDictionary objectForKey:@"product"] TimeAsPrice:[[valuesDictionary objectForKey:@"message_time"]intValue] Avathar:[valuesDictionary objectForKey:@"item_image"] user:[valuesDictionary objectForKey:@"user_image"] Name_User:[valuesDictionary objectForKey:@"user_name"] LocationOfUser:[valuesDictionary objectForKey:@"locate_user"]];
                [objectForArray addObject:shopObject];
            }
            NSLog(@"%@",objectForArray);
            ShopCollectionObject *data = objectForArray[0];
            NSLog(@"%@",data.Name);
        }
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    

    请检查此代码

    【讨论】:

    • 辛苦了,谢谢
    • 不要只发布代码作为答案,请指出有什么不同。
    猜你喜欢
    • 2012-05-28
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多