0
我一直在研究SOAP Web服務,它使用Caché來避免之前完成的HTTP請求。NSDictionary到文件
當我嘗試將NSDictionary保存到文件時,出現錯誤(我想這是因爲它沒有遵循屬性列表結構),我知道如何解決它。
這是我的代碼:
[self loadCache];
NSData *responseData;
NSMutableDictionary* dict = [_cache objectForKey:delegate.type];
if (dict != nil && [dict objectForKey:request.HTTPBody] != nil) {
responseData = [dict objectForKey:request.HTTPBody];
} else {
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
if (dict == nil) {
dict = [[NSMutableDictionary alloc] init];
[_cache setObject:dict forKey:delegate.type];
[dict setObject:[NSNumber numberWithDouble:NSTimeIntervalSince1970] forKey:@"FECHA"];
}
if (responseData == nil) {
responseData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"]];
}
NSLog(@"%@", [[request.HTTPBody copy] class]);
[dict setObject:responseData forKey:request.HTTPBody];
[self saveCache];
}
loadCache功能:
- (void)loadCache {
if (_loadedCache) return;
NSString* docsurl = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
docsurl = [docsurl stringByAppendingPathComponent:@"cache.txt"];
_cache = [[NSMutableDictionary alloc] initWithContentsOfFile:docsurl];
if (_cache == nil) _cache = [[NSMutableDictionary alloc] init];
}
saveCache功能:
- (void)saveCache {
NSString* docsurl = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
docsurl = [docsurl stringByAppendingPathComponent:@"cache.txt"];
if ([_cache writeToFile:docsurl atomically:YES]) {
NSLog(@"SAVED");
} else {
NSLog(@"ERROR");
}
}
解決!我試圖使用NSData作爲KEY,並且它只能用作VALUE。 –