objective-c - NSMutableDictionary 实例变量不在循环外保留键值

标签 objective-c ios nsmutabledictionary

很抱歉,如果这个问题已经出现,但我找不到任何解决我在这里遇到的具体问题的方法。

我在 iOS 上使用 objective-c 工作,我声明了一个 NSMutableDictionary 实例变量,它的一个属性,合成它,并在我的 init 方法中为它分配空间:

SettingsViewController.h

@interface SettingsViewController : UITableViewController <UIAlertViewDelegate> {
    NSMutableDictionary *settingsData;
    UISwitch *emailSwitch;
    UISwitch *phoneSwitch;
    NSMutableData *receivedData;
}

@property (nonatomic, retain) NSMutableDictionary *settingsData;
@property (nonatomic, retain) UISwitch *emailSwitch;
@property (nonatomic, retain) UISwitch *phoneSwitch;
@property (nonatomic, retain) NSMutableData *receivedData;

SettingsViewController.m初始化方法

@synthesize settingsData, emailSwitch, phoneSwitch, receivedData;

# the initialization method, where settingsData is allocated
- (id)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {
        self.navigationItem.title = @"Settings";
        [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc]     initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneEditing:)] autorelease]];
        settingsData = [[NSMutableDictionary alloc] init];
    }
    return self;
}

SettingsViewController.m 中使用 settingsData 的方法的一部分(对于格式为 key=value& 的数据 block ,它只是一个非常简单的解析器):

NSMutableString *element = [NSMutableString string];
NSMutableString *value = [NSMutableString string];
bool cap_element = true;
char index;
for (int i = 0; i < [response length]; i++) {
    index = [response characterAtIndex:i];
    if (cap_element && index != '=') {
        [element appendFormat:@"%c", index];
        continue; // skip back to the top
    }
    if (index == '=') {
        cap_element = false;
        continue;
    }
    if (!cap_element && index != '&') {
        [value appendFormat:@"%c", index];
        continue;
    }
    if (index == '&') {
        // store the value in the dict and move onto the next element
        # this output is always correct...
        NSLog(@"Key:%@, Value:%@", element, value);
        [self.settingsData setObject:value forKey:element];
        # ...as is this (the value printed above matches the result here)
        NSLog(@"Result:%@", [settingsData objectForKey:element]);
        [value setString:@""];
        [element setString:@""];
        cap_element = true;
    }
    # but this will output an empty string (username prints correctly above)
    NSLog(@"%@", [self.settingsData objectForKey@"username"]);

前 2 个 NSLog() 注释按预期输出,但是一旦我离开 for 循环,所有键都保留下来并且它们的值变为空字符串(因此输出读取 username = ""而不是 username = "tester") .如果字典没有初始化,我会理解这一点,但我在上面显示的 init 方法中处理了这一点。我错过了什么?

最佳答案

在字典中,您存储的是对“值”和“元素”变量的引用,而不是副本。当您将它们设置为空字符串时:

[value setString:@""];
[element setString:@""];

你也在更新字典中的值。

编辑:要解决,请更改此行:

[self.settingsData setObject:value forKey:element];

为此创建新的字符串(stringWithString 应该给你自动释放的值):

[self.settingsData setObject: [NSString stringWithString: value] forKey: [NSString stringWithString: element]];

关于objective-c - NSMutableDictionary 实例变量不在循环外保留键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6672501/

相关文章:

objective-c - 核心数据的复杂性

ios - 在类别中实现 UIAlertView 委托(delegate)方法

ios - 奇怪的 Spritekit 碰撞错误取决于 SKSpriteNode 位置

android - Nativescript:带有图标和多行标题的按钮

objective-c - 自定义 NSMutableDictionary 类

ios - Objective-C - 分配、复制、保留

ios - 检索特定月份中星期一的数量

ios - iOS 上 UITextField 验证的更好方法是什么?

objective-c - 使用 NSIndexPath 作为 NSMutableDictionary 中的键的问题?

ios - 在 NSMutableDictionary 中修改 NSMutableArray 的有效方法