2012-05-03 75 views
0

我設置了一個對象類來創建我的pList,但是我遇到了一些問題。我在課堂上使用單例設計模式,所以我只需要在任何時候處理它的一個實例...試圖創建pList但是它沒有被創建

由於一些奇怪的原因它已停止正常工作,對於我的生活我無法圖爲什麼,我woundering如果它有什麼用它做一個Singleton設計模式是..

#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

您設置的值,但不寫入文件。

[savedEnginePropertiesDict writeToFile: plistPath atomically: YES]; 
+0

該死的,這似乎並沒有工作要麼...我甚至嘗試從模擬器刪除應用程序,並重建它,因此它保存到一個新的捆綁包,但也沒有幫助。 –

+0

工作!我在錯誤的地方使用它..沒有注意:( –