NSDictionary 错误
我正在尝试从 NSDictionary 生成 UIActionSheet。
codeCountryMap 是 .h 文件中定义的 NSDictionary 变量。代码编译正确,但在运行时崩溃。但是,当在handleEvents方法中完成初始化时,整个代码都有效。
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *codes = [NSArray arrayWithObjects:@"91", @"01", @"002", nil];
NSArray *cName = [NSArray arrayWithObjects:@"ABC", @"QWE", @"XYZ", nil];
codeCountryMap = [NSDictionary dictionaryWithObjects:codes forKeys:cName];
}
-(IBAction) handleEvents:(id)sender
{
UIActionSheet *displayCodeCountryMap = [[UIActionSheet alloc] initWithTitle:@"Select Country" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil,nil];
for(id key in codeCountryMap) {
[displayCodeCountryMap addButtonWithTitle:(NSString *)key];
}
[displayCodeCountryMap addButtonWithTitle:@"Cancel"];
displayCodeCountryMap.cancelButtonIndex = [codeCountryMap count];
displayCodeCountryMap.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[displayCodeCountryMap showInView:self.view];
[displayCodeCountryMap release];
[country resignFirstResponder];
}
当调用handleEvents:方法时,应用程序崩溃。 任何形式的帮助将不胜感激。
提前致谢。
I am trying to generate an UIActionSheet from a NSDictionary.
codeCountryMap is NSDictionary variable defined in .h file. The code compiled correctly but crashes on run time. But the whole code works when the initialization is done in the handleEvents method
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *codes = [NSArray arrayWithObjects:@"91", @"01", @"002", nil];
NSArray *cName = [NSArray arrayWithObjects:@"ABC", @"QWE", @"XYZ", nil];
codeCountryMap = [NSDictionary dictionaryWithObjects:codes forKeys:cName];
}
-(IBAction) handleEvents:(id)sender
{
UIActionSheet *displayCodeCountryMap = [[UIActionSheet alloc] initWithTitle:@"Select Country" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil,nil];
for(id key in codeCountryMap) {
[displayCodeCountryMap addButtonWithTitle:(NSString *)key];
}
[displayCodeCountryMap addButtonWithTitle:@"Cancel"];
displayCodeCountryMap.cancelButtonIndex = [codeCountryMap count];
displayCodeCountryMap.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[displayCodeCountryMap showInView:self.view];
[displayCodeCountryMap release];
[country resignFirstResponder];
}
The app crashes when the handleEvents: method is called.
Any kind of help would be greatly appriciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很可能是内存管理问题。看起来您正在将 iVar codeCountryMap 设置为 NSDictionary 的自动释放对象,并且不保留它(至少在此代码片段中不保留)。这意味着当调用“handleEvents”方法时,NSDictionary 项已从内存中释放。
尝试保留 NSDictionary 项,或者,如果 codeCountryMap 被定义为保留属性,则使用以下代码来设置它。
这将使用合成方法来设置属性并保留它。
This more than likely is a memory management issue. It looks like you are setting the iVar codeCountryMap to an autoreleased object of NSDictionary and not retaining it(at least not in this code snippet). This means that when the 'handleEvents' method is called the NSDictionary item has been released from memory.
Try either retaining the NSDictionary item or, if codeCountryMap is defined as a retained property, then use the following code to set it.
This will use the synthesized methods to set the property and retain it.
otherButtonTitles 参数中不需要 2 个 nil 终止符。如果您传入了字符串,那么它必须以 nil 终止。既然你没有,一个
nil
就足够了。No need for 2
nil
s terminators in theotherButtonTitles
argument. If you had passed in strings then it would have to be nil terminated. Since you did not, onenil
is enough.