爲什麼當你調用這個方法(saveObject:(id)object forKey:(NSString *)key)時,會不會創建一個文件???創建文件並在其中保存對象
當我把它叫做filepath是等於零,因此( - (空)setFilePath:(的NSString *)文件名)方法將被調用......
-(int) saveObject: (id)object forKey: (NSString *) key {
//save object into file
if(filePath == nil) {
[self setFilePath:nil];
}
mainDict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
if(mainDict == nil) {
mainDict = [[NSMutableDictionary alloc] init];
}
[mainDict setObject:object forKey:key];
if(![mainDict writeToFile:filePath atomically:YES]) {
if(object == nil) {
return 3;
}
if(key == nil) {
return 4;
}
return 2; //ERROR could not write object to file
} else {
if(shouldUseCache == YES) {
cacheStatus = 2; //New Caches need to be updated
}
return 1; //SUCCESS object saved to file
}
//RETURN KEY's
// *1 SUCCESS object saved to file
// *2 ERROR could not write object to file
// *3 ERROR object variable = nil
// *4 ERROR key variable = nil
}
-(void) setFilePath: (NSString *)fileName {
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
if(fileName == nil) {
fileName = @"savedObjects";
}
filePath = [NSString stringWithFormat:@"%@/%@.plist",documentsDirectory, fileName];
}
爲什麼標記'[C]'? – Jack
你真的需要考慮內存管理 –
@max_:爲什麼?如果他使用ARC,對我來說一切似乎都很好。 –