目標是創建一個名爲FetchData.h/.m的單例數據控制器類,該類使用ObjectiveFlickr(https://github.com/lukhnos/objectiveflickr)提取數據。從ObjectiveFlickr在iPhone上的Singleton中存儲數據
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"]);
}
}
當我讀這個文件回來,我得到N ULL。該文件被讀回這樣的:
NSDictionary *inResponseDictionary = [NSDictionary dictionaryWithContentsOfFile:@"inResponseDictionary.xml"];
NSDictionary *photoDict = [[inResponseDictionary valueForKeyPath:@"photos.photo"] objectAtIndex:0];
NSLog(@"%@", [photoDict count]);
有沒有更好的辦法從ObjectiveFlickr存儲這些數據,以便它可以通過其他類和視圖控制器來訪問?還是有更好的方法來在View Controller中實現這一點。
理想情況下,應用程序會將數據保存到應用程序沙箱中的磁盤。雖然這不需要單例,但仍然需要持久性。 – 2011-05-28 01:22:30
這不返回,所以我認爲這就是答案。去嘗試,只是保存圖像數據disk.if([inResponseDictionary將writeToFile:@ 「inResponseDictionary.xml」 原子:YES]) \t {的NSLog(@ 「是」);} \t其他{ \t \t的NSLog(@ 「沒有」); \t} – 2011-05-28 01:25:12