尝试创建pList但是没有创建它

时间:2012-05-03 22:26:38

标签: iphone ios plist

我已经设置了一个我用来创建pList的对象类,但是我遇到了一些问题。我在类上使用单例设计模式,所以我只需要在任何时候处理它的一个实例......

由于一些奇怪的原因,它已经停止正常工作,并且在我的生活中我无法弄清楚为什么,我想知道它是否与它采用单一设计模式有关...

#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 (where we will store and fine our plist in the future)
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        // get documents path
        NSString *documentsPath = [paths objectAtIndex:0];
        // get the path to our plist file
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"];

        NSLog(@"pList path = %@", plistPath);

        // check to see if .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 plist into dictionary object (this is where any saved values get put into 
        savedEnginePropertiesDict = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];

        //Load all current values of the property list thats been put into savedEnginePropertiesDict into their variables
            if (savedEnginePropertiesDict && [savedEnginePropertiesDict count]){
                // assign values
                self.sig = [savedEnginePropertiesDict objectForKey:@"Signature"];
                self.ver = [savedEnginePropertiesDict objectForKey:@"Version"];
                self.num = [savedEnginePropertiesDict objectForKey:@"Number"];
                self.dataV = [savedEnginePropertiesDict objectForKey:@"Data"];
                self.cache = [savedEnginePropertiesDict objectForKey:@"Cache"];

        }
    }
    return self;
}

这应该是创建plist应该驻留的目录,然后如果它在那里读取它,否则创建它..但它没有做任何这些...而我完全失去了解决为什么它不工作

1 个答案:

答案 0 :(得分:1)

您设置值,但不写入文件。

[savedEnginePropertiesDict writeToFile: plistPath atomically: YES];