0

我已经设置了一个用于创建 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应该驻留的目录,然后如果它在那里读取它,否则创建它..但它没有做任何事情..我完全不知道为什么它不工作。

4

1 回答 1

1

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

[savedEnginePropertiesDict writeToFile: plistPath atomically: YES];
于 2012-05-03T22:31:50.727 回答