【发布时间】:2011-09-14 08:48:22
【问题描述】:
我正在努力使用以下代码。我不认为它有什么问题。 代码针对崩溃进行了注释:
- (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];
}
}
我想知道从 [NSControl stringValue] 返回的 NSString* 是返回一个指向内部字符串 reperesentaion 的指针,还是一个新的 NSString,如果我稍后编辑 NSTextField 的文本,它不会改变。
【问题讨论】:
-
它无声地崩溃?没有任何异常或错误?你用断点测试了吗?
-
你能在调用stringValue之后检查调试器的presetParam类型吗?另外,请向我们展示任何崩溃日志输出
标签: objective-c cocoa nsmutabledictionary