0

別のクラスから呼び出される plist のコントローラー クラスを作成しています。コントローラ クラスには read メソッドと save メソッドがあり、read メソッドは plist を開き、読み書き用にドキュメント ファイルに保存します。この読み取りクラスでは、以前に保存したすべての値を独自の変数に入れました。

次に、別のクラスから呼び出される Save メソッド呼び出しで、すべての値がパラメーターとして渡され、そこから新しい値を正しい変数に渡し、それを plist に渡します。ただし、plist 内に辞書があります。そして、1つの値のみを保存し、他の値を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

これらの3つの変数man、mod、subに正しい値を保持する方法を見つけようとしているので、それらをキャッシュ値ディクショナリに入れることができます..

4

2 に答える 2

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;
}

もう1つの問題は、readPlistDataでキー「Man」、「Mod」、および「SubMod」を読み取っていますが、辞書に書き込むものと同じである必要があります:「Manufacturers」、「Models」、および「SubModels」。(どちらも正しい可能性がありますが、同じキーを読み書きできるように一致する必要があります!)

最後に、EngineProperties呼び出すたびに同じインスタンスを使用していますsaveDataか、それとも毎回新しいオブジェクトを作成していますか?同じオブジェクトを使用していない場合、iVarは呼び出すたびにnilにリセットされます。

于 2012-04-05T03:49:47.217 に答える
0

まず電話するときは

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

これを参照して、文字列値を比較します

 //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
于 2012-04-05T04:08:36.927 に答える