views:

563

answers:

1

Hi all!

I have a UISegmentedControl and whenever I touch a button it should show an alert with the index of the segment currently selected:

- (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];
}

However, the app crashes when it must create the NSString. But it does not crash when I replace that line with:

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

or similar.

Oh, and here's what the debugger tells me:

[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)

Can anyone help me?

Also, the alert says '(null)' if the selected index is 0 (zero).

Thanks!

+3  A: 

selectedSegmentIndex is likely an integer value, in which case the format string %@ is not the right choice. Try the following instead:

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

More information can be found in Apple's developer documentation on format specifiers, but the gist of it is that %@ is used only for subclasses of NSObject. It works by calling [object description] which returns a string. If you use it on an integer value, you are essentially sending an Objective-C message to something that isn't an object, which results in undefined behaviour (usually a crash).

e.James
ZOMFG that works thanx man
Time Machine
I cannot upvote it right now cuz I've already reached the vot limit. I'll vote it up in 4 hours.
Time Machine
Glad to help :)
e.James