2012-04-10 72 views
0

我有一个plist,我从我的包中读取到一个新的plist对象,我把它放在根目录进行读写。我的问题是我该怎么做,或者当它退出时应用程序执行什么操作,更好的是当应用程序从ios的“多任务”菜单中死亡时会发生什么。如何保持一个plist,所以你不会失去你的价值

当应用程序再次使用时,还有一种方法可以将此plist保存到内存/软件包以备将来使用。

我的代码如下参考。

这里是我的.h

#import <Foundation/Foundation.h> 

    @interface EngineProperties : NSObject { 

    NSString *signature; 
    NSNumber *version; 
    NSNumber *request; 
    NSNumber *dataVersion; 
    NSMutableDictionary *cacheValue; 
    //cachevalue Items 
    NSNumber *man; 
    NSNumber *mod; 
    NSNumber *sub; 

    } 

    @property (copy, nonatomic) NSString *signature; 
    @property (copy, nonatomic) NSNumber *version; 
    @property (copy, nonatomic) NSNumber *request; 
    @property (copy, nonatomic) NSNumber *dataVersion; 
    @property (copy, nonatomic) NSMutableDictionary *cacheValue; 
    //cachevalue Items 
    @property (copy, nonatomic) NSNumber *man; 
    @property (copy, nonatomic) NSNumber *mod; 
    @property (copy, nonatomic) NSNumber *sub; 



    //Singletton 
+ (id)sharedManager; 
    - (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue; 

    @end 

,这里是我的.m

#import "EngineProperties.h" 

static EnginePropertiesController *sharedMyManager = nil; 

    @implementation EngineProperties 

    @synthesize signature; 
    @synthesize version; 
    @synthesize request; 
    @synthesize dataVersion; 
    @synthesize cacheValue; 

    @synthesize man; 
    @synthesize mod; 
    @synthesize sub; 



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

     // Data.plist code 
     // 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"]; 

     // 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]; 
     if (!tempRoot) 
     { 
      NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
     } 
     // assign values 
     self.signature = [tempRoot objectForKey:@"Signature"]; 
     self.version = [tempRoot objectForKey:@"Version"]; 
     self.request = [tempRoot objectForKey:@"Request"]; 
     self.dataVersion = [tempRoot objectForKey:@"Data Version"]; 

     man = [cacheValue objectForKey:@"Man"]; 
     mod = [cacheValue objectForKey:@"Mod"]; 
     sub = [cacheValue objectForKey:@"SubMod"]; 

     cacheValue = [tempRoot objectForKey:@"Cache Value"]; 
    } 


    - (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned 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.signature = pSignature; 
     self.version = pVersion; 
     self.request = rNumber; 
     self.dataVersion = dvReturned; 

     //do some if statment stuff here to put the cache in the right place or what have you. 
     if (methodName == @"manufacturers") 
     { 
      self.man = cValue; 
     } 
     else if (methodName == @"models") 
     { 
      self.mod = cValue; 
     } 
     else if (methodName == @"subMod") 
     { 
      self.sub = cValue; 
     } 

     self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys: 
          man, @"Manufacturers", 
          mod, @"Models", 
          sub, @"SubModels", nil]; 


     NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys: 
            signature, @"Signature", 
            version, @"Version", 
            request, @"Request", 
            dataVersion, @"Data Version", 
            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]; 

      NSString *myString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding]; 
      //  NSLog(@"%@", myString); 
     } 
     else 
     { 
      NSLog(@"Error in saveData: %@", error); 
      //  [error release]; 
     } 
    } 


    @end 

回答

0

老老实实取决于你如何频繁地要求和更改的plist的价值观。例如,我的应用程序只需要检索一次,然后再写几次(没有太多),所以我的所有保存代码都在所述特定方法的末尾。但是,如果你的plist是活的(不断变化的值),建议保存一个你想要保存的数据的引用,它可以从AppDelegate访问。这样,您只需拨打:beginBackgroundTaskWithExpirationHandler:即可 - applicationDidResignActive并保存您的plist数据。 (请注意,如果用户足够快以在完全保存(大的if)之前杀死您的应用程序,则不会保证您的plist的完整性)。

+0

权。我明白你的意思并且有帮助。但另一个我关心的问题实际上是保存数据..当我关闭我的SDK上的应用程序,并检查捆绑..我的plist是空的。我正在摆弄这位plist最后一次持续多久? (何时会被删除?),即当用户关闭它,删除多任务实例或删除他/她的手机的应用程序?谢谢你的答案btw。 – 2012-04-10 04:02:44

+0

只要你使用-beginBackgroundTaskWithExpirationHandler :,你的对象应该坚持下去。如果你的plist是空的,那么你没有做正确的事情,无论是编码还是对象处理。 – CodaFi 2012-04-10 04:16:34

+0

以及我的plist是空的,因为这是我的plist模板放在包(只读不写),我复制到我的文档根读/写..它只是我不知道如何处理我的plist现在在我的根文档文件夹中创建..或者它持续多长时间......我将读取您刚才列出的方法。 – 2012-04-10 04:49:19

0

按照该链接中的打击链接,他们也提供了代码。如何将数据写入/读取到.plist文件。

Plist

+0

是的,我已经做了很多这个家伙在说什么。但是,如果应用程序从多任务菜单中删除,我更关心如何保留我的值?如果你从多任务菜单中删除了plist值,那么plist值会发生什么变化?> – 2012-04-10 20:54:18

+0

如果你从模拟器/设备删除了应用程序,那么添加的值可以从plist删除,直到值不能被删除。 – Thukaram 2012-04-11 12:46:16

0

可以的Plist快速,轻松地保存到NSUserDefaults的

看看这个快速教程:

http://www.cocoadev.com/index.pl?NSUserDefaults

+0

好吧欢呼花花公子将现在看看它:) – 2012-04-10 21:59:03

+0

真棒让我知道 – pdesantis 2012-04-10 22:14:03

+1

好吧,所以我一直在调查这一点,我有一个问题。 您是否将plist保存到NSUserDefaults ..或者您是否将值保存到NSUserDefaults中,这将非常类似于plist的结构。 – 2012-04-11 01:33:59