selectedSegmentIndex使我的应用程序崩溃

时间:2009-08-30 18:59:39

标签: iphone objective-c crash

我有一个UISegmentedControl,每当我触摸一个按钮时,它应该显示一个警报,其中包含当前所选片段的索引:

- (IBAction)bOkayTouched:(id)sender
{
    NSString *msg = [NSString stringWithFormat:@"%@", [scRPSSL selectedSegmentIndex]];
    UIAlertView *lol = [[UIAlertView alloc] initWithTitle:@"Mkay" message:msg delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [lol show];
    [lol release];
}

然而,应用程序在必须创建NSString时崩溃。但是当我用以下代码替换该行时它不会崩溃:

NSString *msg = [NSString stringWithFormat:@"XD"];

或类似。

哦,这是调试器告诉我的内容:

[Session started at 2009-08-30 21:04:38 +0200.]

[Session started at 2009-08-30 21:04:43 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 4630.
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
unable to read unknown load command 0x80000022
(gdb) 

任何人都可以帮助我吗?

此外,如果所选索引为0(零),警报会显示“(null)”。

谢谢!

1 个答案:

答案 0 :(得分:4)

selectedSegmentIndex可能是一个整数值,在这种情况下,格式字符串%@不是正确的选择。请尝试以下方法:

[NSString stringWithFormat:@"%d", [scRPSSL selectedSegmentIndex]];

有关详细信息,请参阅Apple format specifiers上的开发人员文档,但其要点是%@仅用于NSObject的子类。它通过调用返回字符串的[object description]来工作。如果在整数值上使用它,实质上是将Objective-C消息发送到非对象的东西,这会导致未定义的行为(通常是崩溃)。