我一直在研究SOAP网络服务,
当我试图将我的NSDdictionary 保存到一个文件时, 我发现一个错误( 因为它没有遵循属性列表结构), 我知道如何修正它 。
这是我的代码:
[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];
}
保存缓存函数 :
- (void)saveCache {
NSString* docsurl = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
docsurl = [docsurl stringByAppendingPathComponent:@"cache.txt"];
if ([_cache writeToFile:docsurl atomically:YES]) {
NSLog(@"SAVED");
} else {
NSLog(@"ERROR");
}
}