0

我有一个 plist,我从我的包中读取到一个新的 plist 对象,我将它放在根目录中以进行读写。我的问题是我该怎么做,或者应用程序在退出时会做什么,更好的是,当应用程序从 ios 的“多任务”菜单中被杀死时会发生什么。

还有一种方法可以将此 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
4

3 回答 3

0

老实说,这取决于您要求和更改 plist 值的频率。例如,我的应用程序只需要检索一次,然后写几次(没什么),所以我所有的保存代码都在所述特定方法的末尾。

但是,如果您的 plist 是实时的(常量值更改),建议保留对您希望保存的可从 AppDelegate 访问的数据的引用。这样,您可以简单地调用:beginBackgroundTaskWithExpirationHandler:on -applicationDidResignActive并保存您的 plist 数据。

(注意,如果用户在完全保存之前足够快地杀死您的应用程序(如果),则无法保证您的 plist 的完整性)。

于 2012-04-10T03:37:19.320 回答
0

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

列表

于 2012-04-10T04:59:22.400 回答
0

您可以快速轻松地将 plist 保存到 NSUserDefaults

看看这个快速教程:

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

于 2012-04-10T21:53:54.820 回答