Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

ios - My application crashes with this error - 'NSInvalidArgumentException'

I have created a program to retrieve JSON file and it achieved it

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"]];

But My app crashes here with the above error

  [self.objectForArray addObject:shopObject];
    }

}

Updated my shop collection code below

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You likely aren't initializing your objectForArray. So when you try to call addObject, it's calling it on a null object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...