English 中文(简体)
损失的list价值
原标题:plist values being lost

奥凯过去2天,我曾试图把这个人划出。 我有一个控制我财产清单的班子,它被用作单一吨类别,这样它就只能一时即刻进行,而且它有一个其他方法,用来为名单上的人节省价值。

这份名单不管你做什么,都会把 app子从多塔酒吧(即完全关闭)中去掉,然后该名单上的所有价值都被重新set为无效。 然而,该名单并未丢失。

因此,我现在可能想到的是,我把我从文件主编中得出的读写能力价值观夸大了。 我赞同我的法典榜样,你可以把我的错误暴露出来...... 我不敢肯定我还需要做些什么,如果你有任何问题帮助我回答这个问题,那么请让我知道。

任何帮助都会受到极大赞赏,我的孩子们会but你们。

#import "EnginePropertiesController.h"

static EnginePropertiesController *sharedMyManager = nil;

@implementation EnginePropertiesController

@synthesize pSignature;
@synthesize pVersion;
@synthesize rNumber;
@synthesize dVReturned;
@synthesize cacheValue;

@synthesize manu;
@synthesize mod;
@synthesize submod;


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

            // 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"];
            NSLog(@"myplist path read = %@", plistPath);

            // 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];
            manu = [cacheValue objectForKey:@"Manu"];
            mod = [cacheValue objectForKey:@"Mod"];
            submod = [cacheValue objectForKey:@"SubMod"];
            if (tempRoot && [tempRoot count]){
                // assign values
                self.pVersion = [tempRoot objectForKey:@"PSignature"];
                self.pVersion = [tempRoot objectForKey:@"PVersion"];
                self.rNumber = [tempRoot objectForKey:@"RNumber"];
                self.dVReturned = [tempRoot objectForKey:@"DVReturned"];
                cacheValue = [tempRoot objectForKey:@"Cache Value"];
        }
    }
    return self;
}

- (void) saveData:(NSString *)methodName pSignature:(NSString *)TemppSignature pVersion:(NSNumber *)TemppVersion rNumber:(NSNumber *)TemprNumber dVReturned:(NSNumber *)TempdvReturned 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.pSignature = TemppSignature;
    self.pVersion = TemppVersion;
    self.rNumber = TemprNumber;
    self.dVReturned = TempdVReturned;

    //This checks with methodName I would like to save the value from
    //The reason for this is there are 3 different cache values I get at seperate times to save This if statment just makes sure they are in the correct order.
    if ([methodName isEqualToString:@"Getmanu"]) {
        self.manu = cValue; 
    } else if ([methodName isEqualToString:@"GetMod"]) {
        self.mod = cValue;
    } else if ([methodName isEqualToString:@"GetSubMod"]) {
        self.submod = cValue;
    }        

    //Set up cacheValue Dictionary that will be added to the property list root dictionary
    self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys:
                       manu, @"Manu",
                       mod, @"Mod",
                       subMod, @"SubMod", nil];

    //Pass appropriate values into plist
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
                               protocolSignature, @"Protocol Signature",
                               pVersion, @"Protocol Version",
                               rNumber, @"Request Number",
                               dVReturned, @"Data Version returned",
                               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];
    } else {
        NSLog(@"Error in saveData: %@", error);
    }
}


@end
最佳回答

The Problem: nil Values

这里存在一个问题:pVersion被分配两次,而pSignature仍然是

self.pVersion = [tempRoot objectForKey:@"PSignature"];
self.pVersion = [tempRoot objectForKey:@"PVersion"];

这很可能是这样。

self.pSignature = [tempRoot objectForKey:@"PSignature"];
self.pVersion = [tempRoot objectForKey:@"PVersion"];

这反过来又导致节省时间的麻烦:

NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
                           pSignature, @"PSignature",
                           pVersion, @"PVersion",
                           rNumber, @"RNumber",
                           dVReturned, @"DVReturned",
                           cacheValue, @"Cache Value", nil];

这个构造人不知道它有多少论点,只是继续走到<条码>。 如果<代码>pSignature 是零,它将节省一个空字典。

The Fix: Robust NSDictionary Creation

眼前的解决办法是确定<代码>p 签署读书,总是使用<>字典>建造字典:。 最好使用<条码>物体:forKey单独储存每个物体。 如果该物体为零,则该物体在打前会受到同样的考验。

NSMutableDictionary *plistDict = [NSMutableDictionary dictionary];
if (pSignature) [plistDict setObject:pSignature forKey:@"PSignature"];
if (pVersion) [plistDict setObject:pVersion forKey:@"PVersion"];
// ... and so on.

The Alternative: NSCoding

储存的另一个选择是序列化。 见。 这种取舍:虽然储蓄和装货在发挥作用时更为简单,但由此而产生的文件对人来说不太容易读。

Debugging

最后,有三个地方可以提出标识、断点或方言箱,在类似情况下提供帮助:

  1. Log the dictionaries being loaded and the dictionaries being saved. If you open your app and trigger a save immediately, are the dictionaries identical?
  2. Check the first argument sent to nil-terminated methods like arrayWithObjects: and dictionaryWithObjectsAndKeys:
  3. On the simulator, log the path used for saving and open it in Finder. Is there a file there? If it s a property list, you can open it in XCode and see if it has the content you expect.
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签