我用下面的代碼strugglig。我不會不喜歡它有什麼問題。 代碼被註釋相對於崩潰:問題與NSMutableDictionary setObject:forKey - 崩潰
- (IBAction) SavePreset: (id) sender
{
NSString *presetName = [nameCombo stringValue]; // nameCombo is a NSComboBox*
NSUserDefaults *vmDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *projectionPresets = [vmDefaults objectForKey: kVmGeorefPresetKey];
BOOL doSave = YES;
NSString *dictEntry = [projectionPresets objectForKey: presetName];
if (dictEntry) {
// this branch is not tested yet, plan to test when the rest is working.
int userChoice;
userChoice = NSRunAlertPanel(@"Save Preset",
@"This preset (%s) already exists, modify?",
@"OK", @"Cancel", nil, presetName);
doSave = (userChoice == NSAlertDefaultReturn);
if (doSave) {
[nameCombo addItemWithObjectValue: presetName];
}
}
if (doSave)
{
// projParamText is of class NSTextField*
NSString *presetParam = [projParamText stringValue];
// Up to this point, everything works as expected
// But, the following statement crashes silently.
[projectionPresets setObject: presetParam forKey: presetName];
// and the subsequent code is never executed
[savePresetButton setEnabled: NO];
}
}
我不知道他的NSString *從返回[的NSControl stringValue的]返回一個指向內部字符串reperesentaion或一個新的NSString如果我編輯的文本不會改變之後的NSTextField。
它默默崩潰?沒有任何例外或錯誤?你用斷點測試過嗎? – Joze
您可以在調用stringValue之後檢查調試器的presetParam類型嗎?另外,請告訴我們任何崩潰日誌輸出 –