I am getting the following error when trying to save a file to disk:
ERROR IS Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x8d44150 {NSFilePath=/Users/test/Library/Application Support/iPhone Simulator/5.1/Applications/C283C4FF-645A-4C97-BB87-9591ECC57B0D/Library/Caches/newsfeedCache, NSUnderlyingError=0x8d25870 "The operation couldn’t be completed. Is a directory"}
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"test", @"hula", nil];
[archiver encodeObject:dict forKey:kDataKey];
[archiver finishEncoding];
NSLog(@"CACHE PATH IS %@", self.newsfeedCachePath_);
NSError *error = nil;
BOOL success = [data writeToFile:self.newsfeedCachePath_ options:NSDataWritingAtomic error:&error];
if (success && !error){
} else {
FNLog(@"ERROR IS %@", [error description]);
}
[archiver release];
[data release];
Here’s how I am creating the path:
- (NSString *)createDataPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:cacheName];
/* check for existence of cache directory */
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
return dataPath;
}
NSError *error = nil;
/* create a new cache directory */
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
withIntermediateDirectories:NO
attributes:nil
error:&error]) {
DLog(@"Unresolved error %@, %@", error, [error userInfo]);
return nil;
}
NSString *path = [dataPath stringByAppendingPathComponent:cachePath];
return path;
}
What am I doing wrong?
Take Finder, navigate to
Home/Library/Application Support/iPhone Simulator/5.1/Applications/C283C4FF-645A-4C97-BB87-9591ECC57B0D/Library/Caches. IsnewsfeedCachereally a directory?EDIT: [data writeToFile:] expects a file name, not a directory name. if you feed a directory name to it, that’s an error. You have to come up with some kind of scheme for data storage. Without knowing the particulars of your application, I cannot come up with that for you. Are you going to have one file under that cache or many? How are you going to retrieve them later on? And so forth. Those are rhetorical questions. Answer them for yourself, and based on those answers, come up with a file naming scheme.