我的应用程序因此错误而崩溃-'NSInvalidArgumentException'

时间:2018-07-03 11:04:43

标签: ios objective-c json uicollectionview

我创建了一个程序来检索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

2 个答案:

答案 0 :(得分:0)

您可能没有初始化objectForArray。因此,当您尝试调用addObject时,它是在空对象上调用它。

答案 1 :(得分:-1)

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

请检查此代码