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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…