[Solved] My application crashes with this error – ‘NSInvalidArgumentException’


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

pls check this code

2

solved My application crashes with this error – ‘NSInvalidArgumentException’