我有以下代码将对象保存到文件中......
-(int) saveObject: (id)object forKey: (NSString *) key
{
//save object into file
NSLog(@"PRESENT");
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])
{
return 2; //ERROR could not write object to file
if(object == nil) {
return 3;
}
if(key == nil) {
return 4;
}
}
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
}
如您所见,该方法应该清楚地返回一个介于 1 和 4 之间的数字,但是当我在视图控制器中调用该方法时,我得到 0?
最奇怪的部分是我什至没有在我的控制台中收到 NSLog(@"Present") ???
这是什么问题,是什么原因造成的???
这是我的简单视图控制器...
NSLog(@"%d",[mainDict saveObject:@"HELLO" forKey:@"foo"]);
我的 .h 文件也是...
#import <UIKit/UIKit.h>
#import "ObjectSaver.h"
@interface ViewController : UIViewController {
ObjectSaver *mainDict;
}