我正在为我的plist编写一个控制器类,它从另一个类调用。控制器类有一个读取和保存方法,read方法打开plist并将其保存到文档文件中进行读写。在这个读类中,我将之前保存的所有值都放入了自己的变量中。
然后在从另一个类调用的Save方法调用中,所有值都作为参数传入然后从那里我将新值传递给正确的变量并将其传递给plist ..但是我有一个字典在我的plist中,它只保存一个值,并将其他值重置为null ..我想知道如何确保这些变量保持其值?
这是我的.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;
- (void) readPlistData;
- (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue;
@end
这是我的.m
#import "EngineProperties.h"
@implementation EngineProperties
@synthesize signature;
@synthesize version;
@synthesize request;
@synthesize dataVersion;
@synthesize cacheValue;
@synthesize man;
@synthesize mod;
@synthesize sub;
//This opens up and reads the current plist ready to be used
-(void) readPlistData
{
// 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
我试图找出如何在这三个变量中保留正确的值,man,mod,sub,这样我就可以将它们放入我的缓存值字典中。
答案 0 :(得分:2)
问题是当你准备好数据来创建字典时,在这里:
if (methodName == @"manufacturers")
{
self.man = cValue;
}
else if (methodName == @"models")
{
self.mod = cValue;
}
else if (methodName == @"subMod")
{
self.sub = cValue;
}
这不是比较字符串的正确方法(实际上是在比较字符串的地址,因此它们可能永远不会相同),而应该使用它:
if ([methodName isEqualToString:@"manufacturers"])
{
self.man = cValue;
}
else if ([methodName isEqualToString:@"models"])
{
self.mod = cValue;
}
else if ([methodName isEqualToString:@"subMod"])
{
self.sub = cValue;
}
另一个问题是,在readPlistData中,您正在读取“Man”,“Mod”和“SubMod”键,但它们应与您在字典中写入的内容相同:“制造商”,“模型”和“子模型”。 (任何一个都可能是正确的,但它们需要匹配,以便您正在阅读和编写相同的密钥!)
最后,您每次调用EngineProperties
时是使用相同的saveData
实例,还是每次创建新对象?如果您没有使用相同的对象,那么每次调用它时iVars都会重置为nil。
答案 1 :(得分:0)
首先,当你打电话时
- (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue;
参考this比较字符串值
//This opens up and reads the current plist ready to be used
-(void) readPlistData
{
// 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];
man = [cacheValue objectForKey:@"Man"];
mod = [cacheValue objectForKey:@"Mod"];
sub = [cacheValue objectForKey:@"SubMod"];
if (tempRoot && [tempRoot count]){
// assign values
self.signature = [tempRoot objectForKey:@"Signature"];
self.version = [tempRoot objectForKey:@"Version"];
self.request = [tempRoot objectForKey:@"Request"];
self.dataVersion = [tempRoot objectForKey:@"Data Version"];
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 isEqualToString:@"manufacturers") {
self.man = cValue;
} else if (methodName isEqualToString:@"models") {
self.mod = cValue;
} else if (methodName isEqualToString:@"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