【发布时间】:2011-05-27 21:20:42
【问题描述】:
目标是拥有一个名为 FetchData.h/.m 的单例数据控制器类,它使用 ObjectiveFlickr (https://github.com/lukhnos/objectiveflickr) 提取数据。
FetchData.m 用这个抓取数据:
OFFlickrAPIContext *context = [[OFFlickrAPIContext alloc] initWithAPIKey:YOUR_KEY sharedSecret:YOUR_SHARED_SECRET];
OFFlickrAPIRequest *request = [[OFFlickrAPIRequest alloc] initWithAPIContext:context];
// set the delegate, here we assume it's the controller that's creating the request object
[request setDelegate:self];
[request callAPIMethodWithGET:@"flickr.photos.getRecent" arguments:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"per_page", nil]]
然后实现以下委托:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary;
目前我有这段代码可以将 NSDictionary 作为属性列表保存到文件中,以替代单例:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse: (NSDictionary *)inResponseDictionary{
if([inResponseDictionary writeToFile:@"inResponseDictionary.xml" atomically:TRUE])
{
NSLog(@"%@", [[NSFileManager defaultManager] fileExistsAtPath:@"inResponseDictionary.xml"]);
}
}
当我读回这个文件时,我得到了 Null。文件被这样读回:
NSDictionary *inResponseDictionary = [NSDictionary dictionaryWithContentsOfFile:@"inResponseDictionary.xml"];
NSDictionary *photoDict = [[inResponseDictionary valueForKeyPath:@"photos.photo"] objectAtIndex:0];
NSLog(@"%@", [photoDict count]);
有没有更好的方法来存储来自 ObjectiveFlickr 的这些数据,以便其他类和视图控制器可以访问它?或者有没有更好的方法在 View Controller 中实现这一点。
【问题讨论】:
标签: iphone objective-c flickr