NSDictionary 错误

发布于 2024-10-18 01:23:08 字数 1236 浏览 5 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-10-25 01:23:08

这很可能是内存管理问题。看起来您正在将 iVar codeCountryMap 设置为 NSDictionary 的自动释放对象,并且不保留它(至少在此代码片段中不保留)。这意味着当调用“handleEvents”方法时,NSDictionary 项已从内存中释放。

尝试保留 NSDictionary 项,或者,如果 codeCountryMap 被定义为保留属性,则使用以下代码来设置它。

self.codeContryMap = [NSDictionary dictionaryWithObjects:codes forKeys:cName];

这将使用合成方法来设置属性并保留它。

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.

self.codeContryMap = [NSDictionary dictionaryWithObjects:codes forKeys:cName];

This will use the synthesized methods to set the property and retain it.

心奴独伤 2024-10-25 01:23:08

otherButtonTitles 参数中不需要 2 个 nil 终止符。如果您传入了字符串,那么它必须以 nil 终止。既然你没有,一个 nil 就足够了。

UIActionSheet *displayCodeCountryMap = [[UIActionSheet alloc] initWithTitle:@"Select Country" delegate:self cancelButtonTitle:nil 
                                          destructiveButtonTitle:nil otherButtonTitles:nil];

No need for 2 nils terminators in the otherButtonTitles argument. If you had passed in strings then it would have to be nil terminated. Since you did not, one nil is enough.

UIActionSheet *displayCodeCountryMap = [[UIActionSheet alloc] initWithTitle:@"Select Country" delegate:self cancelButtonTitle:nil 
                                          destructiveButtonTitle:nil otherButtonTitles:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文