Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to maintain a plist so you dont lose your values
    primarykey
    data
    text
    <p>I have a plist which I read from my bundle into a new plist object that I put in the root directory for reading and writing. My question is what do I do with this or what dose the application do when its quit, and better yet what happens when the app is killed from the "multi task" menu from ios.</p> <p>Also is there a way to save this plist to memory/the bundle for future use when the application is used again.</p> <p>My code is as follows for refrence.</p> <p>Here is my .h</p> <pre><code> #import &lt;Foundation/Foundation.h&gt; @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 </code></pre> <p>and Here is my .m</p> <pre><code> #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:&amp;format errorDescription:&amp;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:&amp;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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload