gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:11 25 4
gpt4 key购买 nike

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

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com