この問題が既に発生している場合は申し訳ありませんが、私がここにいる特定の問題に対処するものは見つかりませんでした。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のinitメソッド
@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;
}
settingsDataを使用SettingsViewController.m方法のポーション(それはチュですSTデータのブロックのための非常にシンプルなパーサフォーマットされたキー=値&):予想、しかし、すぐに私はループのためにすべてのキーを残すようが残るよう
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 [email protected]"username"]);
最初の2のNSLog()コメント出力とそれらの値は空の文字列になります(したがって、username = "tester"の代わりにusername = ""と出力されます)。私は辞書が初期化されていない場合、これを理解するだろうが、私は上記のinitメソッドでそれを世話する。私は何が欠けていますか?
私の神は本当にありがとう、私はそれを逃したとは思えない – drodman