0

I am using a NSString output of a NSMutableArray to put as text for a UIAlertView, to appear as the default text.

I find that I have to recreate an NSString from the NSString coming from the dictionary, otherwise it creates an 'NSInvalidArgumentException'.

Specifically, when I call:

NSMutableArray *serverResponseNumbers;
...
UIAlertView *alertviewmod=[[UIAlertView alloc] initWithTitle:@"Modify" message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm",nil];
[alertviewmod setDelegate:self];
alertviewmod.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;  //two text fields
[alertviewmod textFieldAtIndex:0].keyboardType=UIKeyboardTypeNumberPad;
NSString *nsRecordNumber=[serverResponseNumbers objectAtIndex:an_int_variable];
...
[alertviewmod textFieldAtIndex:0].text= nsRecordNumber;

I get this exception:

-[__NSCFNumber _isNaturallyRTL]: unrecognized selector sent to instance 0xde4dbf0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber _isNaturallyRTL]: unrecognized selector sent to instance 0xde4dbf0'

Just to 'prove' it (assuming NSLog proves it), the following works:

NSLog(@"nsRecordNumber=%@",nsRecordNumber);

or the following line works fine it its place:

[alertviewmod textFieldAtIndex:0].text= @"static text";

What I find fixes it, but is rather inelegant, is to recreate the NSString. In my case this string is just of an integer number, so I can use this hack:

     int number=[nsRecordNumber integerValue];
     NSString *nsNumberRecreated=[NSString stringWithFormat:@"%d",number];
     [alertviewmod textFieldAtIndex:0].text= nsNumberRecreated;

But this really shouldn't be needed. And I read that '_isNaturallyRTL' is an undocumented function. Can anyone explain this? Perhaps something wrong I'm doing, or a bug? I am using XCode 4.6, targeting iOS 4.3, and testing on an iPad with iOS 5.1.1.

Hemang
  • 26,840
  • 19
  • 119
  • 186
Mark
  • 41
  • 3
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Feb 01 '15 at 23:21
  • You're trying to use a NSNumber to set the `text` property which is declared to expect an NSString. – Hot Licks Feb 01 '15 at 23:23
  • My apologies, I used a confusing variable name with 'Number' inside the NSString variable name. It is an NSString, not a number or NSNumber that I am adding to the Alert text box. – Mark Mar 02 '15 at 04:59
  • No, you are adding an NSNumber, as can be clearly seen from the exception message. – Hot Licks Mar 02 '15 at 12:27

1 Answers1

0

My best guess is that nsRecordNumber is actually a NSNumber instead of a NSString. That would explain why the NSLog works but setting the text doesn't. If that is a case you can do.

[alertviewmod textFieldAtIndex:0].text= [NSString stringWithFormat:@"%@", nsRecordNumber];

That is basically what you are doing in your NSLog.

Edit: A better choice would be.

[alertviewmod textFieldAtIndex:0].text= [nsRecordNumber stringValue];

Try doing this to very that it is actually a NSString or NSNumber.

if ([nsRecordNumber isKindOfClass:[NSNumber class]])
{
    NSLog(@"it is a number");
}
else if([nsRecordNumber isKindOfClass:[NSString class]])
{
    NSLog(@"it is a string");
}

Hopefully that helps fix the issue. As far as where _isNaturallyRTL is coming from I do not know.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30
  • One should not use `description` to generate "user" text -- except for some specific classes, description is intended only for diagnostics. Instead use the appropriate method (`intValue`, `floatValue`, etc) of NSNumber to extract the value, then the appropriate `%` formatting code to format it. – Hot Licks Feb 02 '15 at 02:36
  • @HotLicks is correct using the description isn't an ideal way to handle this and I updated my answer. There is however a stringValue method on NSNumber you can use or go about it the way he suggested. I would recommend checking nsRecordNumber and verify if it is a NSString as you originally thought or a NSNumber as I suspect. – Skyler Lauren Feb 02 '15 at 02:50