plist值丢失了

时间:2012-04-11 23:07:03

标签: iphone ios plist

好的,我花了最近两天的时间试图对这个进行排序。我有一个控制我的属性列表的类,它被用作单例类,所以它只被实例化一次,并且它有一个其他方法用于将值保存到plist。

无论你做什么,plist工作都很好,直到你从多任务栏中删除应用程序(即完全关闭应用程序)然后plist中的所有值都被重置为null ..但是plist不会丢失。

所以我现在想的是,我可能会覆盖我在plist中的值,这些值是我在根文档目录中为读写功能所做的。我希望我的代码示例你们可能能够发现我的错误...我不确定我还需要添加什么......如果你有任何问题可以帮助我帮助我回答这个问题那么请让我知道。

任何帮助都将受到极大的赞赏,我的孩子的孩子将在你的首次亮相。

#import "EnginePropertiesController.h"

static EnginePropertiesController *sharedMyManager = nil;

@implementation EnginePropertiesController

@synthesize pSignature;
@synthesize pVersion;
@synthesize rNumber;
@synthesize dVReturned;
@synthesize cacheValue;

@synthesize manu;
@synthesize mod;
@synthesize submod;


#pragma mark Singleton Methods
+ (id)sharedManager {
    @synchronized(self) {
        if (sharedMyManager == nil)
            sharedMyManager = [[self alloc] init];
    }
    return sharedMyManager;
}
- (id)init {
    if (self = [super init]) {

            // get paths from root direcory
            NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
            // get documents path
            NSString *documentsPath = [paths objectAtIndex:0];
            // get the path to our Data/plist file
            NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"];
            NSLog(@"myplist path read = %@", plistPath);

            // check to see if Data.plist exists in documents
            if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
            {
                // if not in documents, get property list from main bundle
                plistPath = [[NSBundle mainBundle] pathForResource:@"EngineProperties" ofType:@"plist"];

            }

            // read property list into memory as an NSData object
            NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
            NSString *errorDesc = nil;
            NSPropertyListFormat format;
            // convert static property liost into dictionary object
            NSDictionary *tempRoot = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
            manu = [cacheValue objectForKey:@"Manu"];
            mod = [cacheValue objectForKey:@"Mod"];
            submod = [cacheValue objectForKey:@"SubMod"];
            if (tempRoot && [tempRoot count]){
                // assign values
                self.pVersion = [tempRoot objectForKey:@"PSignature"];
                self.pVersion = [tempRoot objectForKey:@"PVersion"];
                self.rNumber = [tempRoot objectForKey:@"RNumber"];
                self.dVReturned = [tempRoot objectForKey:@"DVReturned"];
                cacheValue = [tempRoot objectForKey:@"Cache Value"];
        }
    }
    return self;
}

- (void) saveData:(NSString *)methodName pSignature:(NSString *)TemppSignature pVersion:(NSNumber *)TemppVersion rNumber:(NSNumber *)TemprNumber dVReturned:(NSNumber *)TempdvReturned cacheValue:(NSNumber *)cValue
{
    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"];

    // set the variables to the values in the text fields
    self.pSignature = TemppSignature;
    self.pVersion = TemppVersion;
    self.rNumber = TemprNumber;
    self.dVReturned = TempdVReturned;

    //This checks with methodName I would like to save the value from
    //The reason for this is there are 3 different cache values I get at seperate times to save This if statment just makes sure they are in the correct order.
    if ([methodName isEqualToString:@"Getmanu"]) {
        self.manu = cValue; 
    } else if ([methodName isEqualToString:@"GetMod"]) {
        self.mod = cValue;
    } else if ([methodName isEqualToString:@"GetSubMod"]) {
        self.submod = cValue;
    }        

    //Set up cacheValue Dictionary that will be added to the property list root dictionary
    self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys:
                       manu, @"Manu",
                       mod, @"Mod",
                       subMod, @"SubMod", nil];

    //Pass appropriate values into plist
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
                               protocolSignature, @"Protocol Signature",
                               pVersion, @"Protocol Version",
                               rNumber, @"Request Number",
                               dVReturned, @"Data Version returned",
                               cacheValue, @"Cache Value", nil];


    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData) {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
    } else {
        NSLog(@"Error in saveData: %@", error);
    }
}


@end

1 个答案:

答案 0 :(得分:2)

问题:零值

这是一个问题:pVersion被分配两次,而pSignature仍然是nil

self.pVersion = [tempRoot objectForKey:@"PSignature"];
self.pVersion = [tempRoot objectForKey:@"PVersion"];

这应该是

self.pSignature = [tempRoot objectForKey:@"PSignature"];
self.pVersion = [tempRoot objectForKey:@"PVersion"];

这反过来会导致节省时间的麻烦:

NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
                           pSignature, @"PSignature",
                           pVersion, @"PVersion",
                           rNumber, @"RNumber",
                           dVReturned, @"DVReturned",
                           cacheValue, @"Cache Value", nil];

这个构造函数不知道它有多少个参数,只是一直持续到nil。如果pSignature为零,则会保存一个空字典。

修复:强大的NSDictionary创建

虽然立即解决方案是修复pSignature读数,但使用dictionaryWithObjectsAndKeys:构建字典总是很脆弱。使用setObject:forKey单独存储每个对象要好得多。如果对象为nil,则抛出异常,因此在调用之前进行测试。

NSMutableDictionary *plistDict = [NSMutableDictionary dictionary];
if (pSignature) [plistDict setObject:pSignature forKey:@"PSignature"];
if (pVersion) [plistDict setObject:pVersion forKey:@"PVersion"];
// ... and so on.

替代方案:NSCoding

存储的另一个选择是序列化。请参阅Archives and Serializations Programming Guide。这需要权衡:虽然保存和加载在工作时更简单,但生成的文件不易被人类阅读。

调试

最后,有三个地方放置日志语句,断点或对话框,这有助于这样的情况:

  1. 记录正在加载的词典和正在保存的词典。如果您打开应用程序并立即触发保存,字典是否相同?
  2. 检查发送给nil终止方法的第一个参数,例如arrayWithObjects:dictionaryWithObjectsAndKeys:
  3. 在模拟器上,记录用于保存的路径并在Finder中将其打开。那里有档案吗?如果它是属性列表,您可以在XCode中打开它,看它是否具有您期望的内容。
相关问题