Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSMutableDictionary的奇怪重置_Iphone_Objective C_Nsmutabledictionary - Fatal编程技术网

Iphone NSMutableDictionary的奇怪重置

Iphone NSMutableDictionary的奇怪重置,iphone,objective-c,nsmutabledictionary,Iphone,Objective C,Nsmutabledictionary,我有以下功能: - (NSMutableDictionary *) getUserDataDictionary { [userDataDicionary removeAllObjects]; userDataDicionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getUserDataDictionaryPath]]; return userDataDicionary; } -

我有以下功能:

- (NSMutableDictionary *) getUserDataDictionary
{
    [userDataDicionary removeAllObjects];
    userDataDicionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getUserDataDictionaryPath]];

    return userDataDicionary;   
}

- (int) getIndexOfLastVehicle
{
MyAppDelegate *app = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *tmpUserData = [app getUserDataDictionary];

int lastHighestIndex = -1;

for(id item in [tmpUserData allKeys]){
    NSString *keyInArray = (NSString *)item;

    if ([keyInArray rangeOfString:@"VEHICLE-"].location != NSNotFound) {
        //f.e. "VEHICLE", "1", "TYPE"...or "VEHICLE", "1", "SPZ"...or "VEHICLE", "2", "TYPE" etc
        NSArray * separatedComponents = [keyInArray componentsSeparatedByString:@"-"];
        int indexOfVehicle = [(NSString *)[separatedComponents objectAtIndex:1] intValue];

        if(indexOfVehicle > lastHighestIndex){
            lastHighestIndex = indexOfVehicle;
        }
    }
}

return lastHighestIndex;
}
问题是: 在此代码之后:

MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *tmpUserData = [app getUserDataDictionary];

int lastVehicleIndex = [self getIndexOfLastVehicle];
tmpUserData为空

但当我改变顺序时:

int lastVehicleIndex = [self getIndexOfLastVehicle];

MyAppDelegate *app = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *tmpUserData = [app getUserDataDictionary];
tmpUserData已正确填写

有人能解释这种行为吗?
谢谢

在Objective C中,我远不是专业人士,但另一方面,我在az NSMutableArray中也遇到了类似的问题。在我的例子中,问题是某些东西被释放或初始化的次数比要求的多一次

我建议在各处放置一些NSLog-s,以找出您的内容丢失的地方。我这样做了,我基本上在涉及我的对象的每一条指令上都放了一个NSLog,最后我能够修复这些东西


这只是一个想法,可能会有所帮助。

您在这种方法中遇到了问题:

- (NSMutableDictionary *) getUserDataDictionary
{
    [userDataDicionary release]; 
    userDataDicionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getUserDataDictionaryPath]];

    return userDataDicionary;   
}

您的问题之一在于getUserDataDictionary方法。您正在调用removeAllObjects,它不会释放userDataDictionary。您必须释放它,而不是移除AllObject。实际版本将为您发布其所有对象。

顺便说一句,根据苹果的编码约定,有一条规则将属性设置器命名为“setProperty”,将getter命名为“property”,因此最好将该方法重命名为UserDataDictionaryies,这就是问题所在。谢谢大家。