我试图将数据从我的JSON文件传递到一个简单的ViewController上,但我不知道将数据真正传递到哪里。我只能添加到我的setDataToJson
方法中,还是应该将数据添加到我的viewDidLoad
方法中?
以下是我的代码
@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{
NSData* data = [NSData dataWithContentsOfFile:fileLocation];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
@implementation ViewController
@synthesize name;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)setDataToJson{
NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"];
name.text = [infomation objectForKey:@"AnimalName"];//does not pass data
}
https://stackoverflow.com/questions/10866403
复制相似问题