Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...