尝试创建 pList,但它没有被创建

  • 本文关键字:创建 pList 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 应该驻留的目录,然后如果它在那里读取它,否则创建它......但它没有做任何这些......我完全不知道为什么它不起作用。

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

[savedEnginePropertiesDict writeToFile: plistPath atomically: YES];

最新更新