NSNull length crash with textField, - ios

when i enter any character or number with keyboard.the app crash with this info
"Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized
selector sent to instance 0x103b5daf0'".
And every textfield that used in the app has this problem.The textfield is in a storybord-based appliction.this is the textfield delegate i overwrite and this is the exception throw call stack

The problem is probably that you somewhere set the textField's text property to NSNull (which is a bug).
It's not (like other answers state) the comparison [textField.text isEqual:[NSNull null]] that leads to the bug. This line is nonsense, but can't result in the crash.
Search for places where you set the textfield's text property and check that the value is always of type NSString.

replace ([textField.text isEqual:[NSNull null]]) with (textField.text.length == 0).

According to your exception throw call stack, this crash is caused of by this method [textfield.text isEqual:[NSNull null]]`. You can use this code:
if (textfield.text){
}
Or
if ([textfield.text isEqualToString:#"your text"]){
}
textfield.text is an object which class is NSString and [NSNull null] is also an object that represent empty object.

Related

Error when adding NSString object to my NSMutableArray

I am getting this error when trying to add the NSString object named object to my array :
erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fc48ae52ba0'
Not sure why this is so. I was originally using NSArray to changed it to NSMutableArray but I am still having problems with it. Code is below:
-(NSMutableArray *)getReplyArrayForMessage: (NSString *)message {
//get the array
NSMutableArray *replies = [self.messageDictionary objectForKey:message];
NSLog(#"%#",replies);
//if no replies we init the base set
if([replies count]==0) {
//get the base array
//this also works if a key just isn't in the dictonary
return replies=[self getBaseArray];
}
else {
//add the other message
NSString *object = #"Send a different message";
[replies addObject:object];
return replies;
}
}
If anyone could give me a pointer to why this is happening I would appreciate it. Noob here.
The array is immutable (NSArray), not mutable (NSMutableArray). You can create a mutable array from an immutable array using mutableCopy:
NSMutableArray *replies = [[self.messageDictionary objectForKey:message] mutableCopy];
I know this from the exception text:
-[__NSArrayI addObject:]: unrecognized selector sent to instance ...
^^^^^^^^^^
EDIT I've missed off some important information from my original answer.
Having used mutableCopy you now have a copy of the array and the original array (from self.messageDictionary objectForKey:message) will remain unchanged after you add your element. This is almost certainly not what you intended.
As I've mentioned in the comments below, you probably want to create a custom object to hold these details (or an array of custom objects) that should be created from the message dictionary as soon as you receive it. The effort required to create a custom object will pay for itself tenfold in the long term.
You are trying to add an NSString to an NSArray (which looks like an NSMutableArray) since you initialized it as one. This means that the object that you store in your messageDictionary is actually of type NSArray, and not NSMutabelArray. So, assigning it to an object of type NSMutableArray won't actually change its type and make it mutable.
It's an easy fix:
NSMutableArray *replies = [[NSMutableArray alloc] initWithArray:(NSArray *) [self.messageDictionary objectForKey:message]];
Or you can go with mutableCopy (as suggested by #trojanfoe) which is a shortcut but will lead to the same result.
I can tell because the error message says -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fc48ae52ba0'. Notice the I at the end of __NSArrayI, this means that this array is actually immutable, so you can't add objects to it.

UILabel in a NSMutableArray unrecognized selector

In my app, I have created a 'NSMutalbeArray', in which I add some 'UILabel'.
I correctly initialize my array :
_pickerMonthLabel = [[NSMutableArray alloc] init];
I correctly add UILabels in my array, but when I want to make some changes to a label, the behavior is strange. For example, I want to change the text color of a label in my array. I do :
[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]];
This doesn't look like to work because my app crashes :
-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4
*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4'
But i have no warning when I wrote this line. When I write another kind of line :
(UILabel *)[_pickerMonthLabel objectAtIndex:i].textcolor = [UIColor redColor];
It gives me an error :
Property 'textcolor' not found on object of type 'id'
I don't understand the difference between these two lines, and how to fix my problem...
Thank you in advance for your help.
I guess this is more of a remark, but you could try to use the respondsToSelector: message to check if the instance can handle the message you are trying to send. (This will make sure you don't get an exception) In your case this would be:
if ([[_pickerMonthLabel objectAtIndex:i] respondsToSelector:#selector(setTextColor:)]){
[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]];
}
// add this to see what your class is
//else{
// NSLog(#"The class is: %#", [[_pickerMonthLabel objectAtIndex:i] class]);
//}
Hope it helps your debugging.

Casting tableViewCell as UIPickerView exactly how it is done with UIDatePicker for inline date picker not working

I am attempting to use an inline PickerView in my application, and it works fine except for one thing. when I try to set the row of the picker to a specific row based on a data argument my application crashes.
UITableViewCell *associatedTypePickerCell = [self.tableView cellForRowAtIndexPath:self.datePickerIndexPath];
UIPickerView *targetedPickerView = (UIPickerView *)[associatedTypePickerCell viewWithTag:kTypePickerTag];
if (targetedPickerView != nil){
NSLog(#"type picker ! = nil");
NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row-1];
Type *type = [itemData valueForKey:kDateKey];
NSInteger row = [[self types] indexOfObject:type];
[targetedPickerView selectRow:row inComponent:0 animated:NO];
}
I am getting this error:
-[UITableViewCell selectRow:inComponent:animated:]: unrecognized selector sent to instance 0x8ecba70
2014-07-22 23:47:23.384 MyGIJournal[16636:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell selectRow:inComponent:animated:]: unrecognized selector sent to instance 0x8ecba70'
does anyone know why this is crashing? it seems like casting like this to a UIPickerView isn't working, which doesn't make much sense cause this is the exact same way I do it for the UIDatePicker and it worked for that... Any help or advice would be greatly appreciated, Thanks!

Attributed text in UITextView causing a crash

This is my code to display an attributed string in a UITextView
NSMutableAttributedString *recipeString =[[NSMutableAttributedString alloc]init];
[recipeString appendAttributedString:string1];
[recipeString appendAttributedString:string2];
[array addObject:recipeString];
This code is inside a for loop. string1 and string2 are NSMutableAttributedStrings.
After that:
self.textView.text = [array objectAtIndex:self.appDelegate.selectedCell];
The text view is an IBOutlet.
It crashes with this exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteAttributedString _isCString]: unrecognized selector sent to instance 0x8e866f0'
Any ideas on how to fix this crash?
I had to use the attributedText property of UITextView:
self.textView.attributedText = [array objectAtIndex:self.appDelegate.selectedCell];
For future users:
-[NSConcreteAttributedString mutableString]: unrecognized selector sent to instance
this may also be caused by if in the above instance string1 or string2 is nil

UITextField subclass throwing weird exception at becomeFirstResponder

I have character boxes made of my UINavigatableTextField. After each input, responder character should succeed to it's successor. Weirdly, during input of first character, if user enters character 'Q' each time different exception like;
EXC_BAD_ACCESS, or
2012-09-04 14:42:42.600 Kelime Oyunu[6350:707] -[WebScriptObjectPrivate isForShortcutConversion]: unrecognized selector sent to instance 0x21b870
2012-09-04 14:42:42.606 Kelime Oyunu[6350:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WebScriptObjectPrivate isForShortcutConversion]: unrecognized selector sent to instance 0x21b870'
is thrown. My code receiving exception below, the [next becomeFirstResponder] line
- (void) moveToNextCharacter: (MBNavigatableTextField *) character
{
dispatch_async(dispatch_get_current_queue(),
^{
UIControl *next = [character nextField];
if(next == nil)
{
[character endEditing:YES];
}
else if ([next isKindOfClass:[UIButton class]])
{
[next sendActionsForControlEvents: UIControlEventTouchUpInside];
}
else
{
[next becomeFirstResponder];
}
});
}
How can i solve the problem? Thanks in advance.
Edit 1: Found a zombie [next resignFirstResponder] (Thanks to #PhillipMills)
Edit 2: It turns out my problem caused from
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Something done here...
// code block of evil zombie summoner
[textField setText:#"Some text"];
// Something else done here
}
Profile->Instruments->Zombies stating that during setText: some object is released (which is previous string i guess).
I am still unable to solve the problem. My goal is updating textField above with new user input (replacementString:).
All messaging to UIKit objects has to be done on the main queue. While that may be the case, this statement:
dispatch_async(dispatch_get_current_queue()
is troublesome since we don't know what queue you get the message on. Change it to:
dispatch_async(dispatch_get_main_queue()
so its absolutely clear.
Also, under this line:
UIControl *next = [character nextField];
add these statements:
NSLog(#"Character CLASS %#", NSStringFromClass[character class]);
NSLog(#"next CLASS %#", NSStringFromClass[next class]);
Make sure these are what you think they should be.
EDIT: Regarding the use of "unsafe_unretained": if a IBOutlet is contained within "self.view" - that is the primary view - then you should be using weak on iOS - so it gets nil'd for you when the view goes away.
Regarding 'shouldChangeCharactersInRange', if you are simply looking at the characters, you return YES. If you change the text itself - by writing to the field/view directly, then return NO. If you modify text and then return YES, you're in essence changing data behind the control's back with who knows what consequences.

Resources