Error in string length - ios

I have a table view which may contain number of data.
When user select one particular cell I set Id in string based on that selection. And then I am trying to find length of that string. But I am getting error in that. When I print that string it print its value.
Here is my code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId=[charityArray objectAtIndex:0];
NSLog(#"charityId%#",charityId);
NSLog(#"charitylen%d",[charityId length]);
}
and here is log screen.
2013-12-04 14:47:13.897 GratZeez[2178:a0b] charityId3
2013-12-04 14:47:13.897 GratZeez[2178:a0b] -[__NSCFNumber length]: unrecognized selector sent to instance 0xd174070 2013-12-04 14:47:13.899
GratZeez[2178:a0b] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFNumber length]:
unrecognized selector sent to instance 0xd174070'
declaration of charityId:
#import <UIKit/UIKit.h>
#interface ExsitingCharityViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
NSMutableArray *dataArray,*tempArray,*searchResultArray,*nameSearchArray;
BOOL search;
NSMutableDictionary *dataDict;
CGPoint originalCenter;
NSString *charityId;
}

try this code, Convert NSNumber to NSString and then use length
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId = [charityArray objectAtIndex:0];
NSString *charityString = [charityId stringValue];
NSLog(#"charityString = %#", charityString);
NSLog(#"charityString length = %d",[charityString length]);
}

As you post, charityId s not a NSNumber formatted type value.
If you want to know to length convert in to NSString formate,
Like, NSString* str = [NSString stringwithFormat:#"%#", charityId];
then you get length.

As the error log shows, you are sending the length message to a NSNumber object. According to your post, you seem to be trying to send it to a NSString but it is not a NSString.
-[__NSCFNumber length]: unrecognized selector sent to instance 0xd174070'

If you wish to use the %d string format specifier with an NSNumber you should convert the NSNumber object to an integer first:
NSLog(#"charityId%d",[charityId intValue]);

use this code for NSNumber to NSString
NSString *strId=[NSString StringWithFormat#"%d", charityId];
NSLog(#"charitylen%d",[strId length]);

If you are trying to NSLog an NSNumber do the following, replace this:
NSLog(#"charitylen%d",[charityId length]);
With this:
NSString *myString = [charityId stringValue];
NSLog(#"charitylen%#", myString);
And if you are trying to get the count of your Array use the following:
NSLog(#"charitylen%d",[charityId count]);
Update:
I have to opinions about your case:
1- It means that you are passing an NSNumber where the called code expects an NSString or some other object that has a length method. You can tell Xcode to break on exceptions so that you see where exactly the length method gets called.
2-I think it may because your charityId is empty or never used before you NSLog it .

-[__NSCFNumber length]: unrecognized selector sent to instance it's mean you are tried to get length of a number (i.e. int, NSInteger etc). so you need to typecast.
you can do it either using [NSString stringWithFormat:#"%d", charityId] or [charityId stringValue]
so just do below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId=[charityArray objectAtIndex:0];
NSString *charityIdString=[NSString stringWithFormat:#"%d", charityId];
NSLog(#"charityId%#",charityIdString);
NSLog(#"charitylen%d",[charityIdString length]);
}

Related

Why does Length not work on a NSString?

I am trying to check the length of string, but I get unrecognized selector when the code is executed and hits the IF statement.
THE VALUE IN THE DICTIONARY IS A STRING.
NSString * checkString = [myDictionary objectForKey:#"somekey"];
NSLog(#"length: %lu", (unsigned long)[checkString length]);
if([checkString length] > 0){
}
ERROR From console:
length: 0
[__NSCFNumber length]: unrecognized selector sent to instance
For simplicity:
NSString * checkString = #"my string"; //[myDictionary objectForKey:#"somekey"];
NSLog(#"length: %lu", (unsigned long)[checkString length]);
if([checkString length] > 0){
}
length: 9
[__NSCFNumber length]: unrecognized selector sent to
instance
Why?
The error says that your instance is not a NSString but a NSNumber. That's probably because you stored a NSNumber in [myDictionary objectForKey:#"somekey"].
Try to put [yourValueThatYouThinkItsAString stringValue] in the place where you store this value.
I tried your code and I'm not getting error with it (with #"my string" value).
What you can try :
NSString *checkString = [[myDictionary objectForKey:#"somekey"] stringValue];
NSLog(#"length: %lu", (unsigned long)[checkString length]);
if([checkString length] > 0){
}
PS : in your NSLog() you've got a additional argument, remove it.
The reason the length is not working on NSString* is that it's not NSString*. Despite the cast, which Objective-C cannot verify, the object in your dictionary is NSNumber*, not NSString*. That is why the code compiles, but fails to run.
[myDictionary objectForKey:#"somekey"] call returns id, a generic object reference. That is why Objective-C must trust you when you perform a cast to NSString* that the object at "somekey" is actually a string.
It does not fail in NSLog because you made a mistake that prevents the length from being evaluated. Change NSLog to see it fail:
NSLog(#"length: %lu", (unsigned long)[checkString length]);
If you have XCode 7 or newer, you can use lightweight generics to help Objective-C with type checking. You can specify object types for keys and values in your dictionary, so that the compiler could catch invalid casts for you:
NSDictionary<NSString*,NSString*> *myDictionary = ...

How can we display number values in UILabels in iOS?

I am very new for iOS and in my app I am integrating services and after getting response from service I am storing the values in Array-list.
When I load that array list values in tableList showing exception like:
_Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x7d7ca000'
McoIdArray:-(
106314100491,
106314100492,
106314100493,
106314100494,
106314100495
)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"MyCell";
Cell = (CoridersCell *)[tableList dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CoridersCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}
Cell.mcoId.text = [McoIdArray objectAtIndex:indexPath.row];
return Cell;
}
The problem is you are assigning an NSNumber instance instead of an NSString. You should format the number to a string like this:
NSNumber* number = [McoIdArray objectAtIndex:indexPath.row];
cell.mcoId.text = [NSString localizedStringWithFormat:#"%#", number];
If you want to have more control over number formatting you can use NSNumberFormatter.
Your array contains instances of NSNumber, not NSString. So you can't directly assign the value from the array to the label's text property. You must first convert the NSNumber to an NSString.
One option is to use the stringValue method.
Cell.mcoId.text = [McoIdArray[indexPath.row] stringValue];
Also note the use of modern syntax to access the array.
Replace the line:
Cell.mcoId.text = [McoIdArray objectAtIndex:indexPath.row];
with the line I show above.

Unrecognized selector sent to instance On UILabel

I am parsing JSON and get the value.Store value as NSString and when I want to show on UILabel app crashes with
unrecognized selector sent to instance 0x7b070470`
NSString *idvalue = [jsonResponse objectForKey:#"id"];`
labl_id.text =idvalue;
NSLog(#"id value%#",idvalue);
output: id value 6
please let me know how to fix this
thanks
mb you get NSNumber
NSString *idvalue = [[jsonResponse valueForKey:#"id"] stringValue];

NSDecimalNumbers in Array Causing "Unrecognized Selector..." messages

Ive been playing around with obj-c for a few months now and feel comfortable with a lot of things but cant make sense of something that seems very simple. I need to be able to go from an NSString split into an array then get the NSDecimalNumbers from that array. If I try to perform any calculations, I get an "unrecognized selector sent to instance." If I simply print each decimal number everything goes smoothly. If I put the values manually into NSDecimalNumbers rather than pulling from the array that also works. The error and code are below.
2013-10-14 15:54:42.174 test[30829:c07] -[__NSCFString
decimalNumberByAdding:]: unrecognized selector sent to instance
0x75a81f0 2013-10-14 15:54:42.176 test[30829:c07] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString decimalNumberByAdding:]: unrecognized selector sent to
instance 0x75a81f0'
* First throw call stack: (0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0x2075 0x10e1705 0x152c0 0x15258 0xd6021 0xd657f 0xd56e8
0x44cef 0x44f02 0x22d4a 0x14698 0x1bebdf9 0x1bebad0 0x1c05bf5
0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x11ffc
0x1bfd 0x1b25) libc++abi.dylib: terminate called throwing an exception
-(IBAction)buttonPushed: (id) sender
{
NSString* s = #"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: #" "];
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
NSDecimalNumber* dec2 = [a objectAtIndex: 1];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum, nil];
}
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
doesn't give you a NSDecimalNumber object. It gives you a string (which was created via the call to componentsSeparatedByString)
Try doing this:
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString: [a objectAtIndex: 0]];
and see if you have better luck.
you never created a NSDecimalNumber object
NSArray* a = [s componentsSeparatedByString: #", "];
will of course create an aray of strings.
Just because you type the pointer as decimal number, doesnt make the obejct to be one
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
dec1 is still a NSString. Try
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 0]];
NSDecimalNumber* dec2 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 1]];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum];
When you call [a objectAtIndex: 0] that is just returning a string, that you're trying to cast to a NSDecimalNumber.
Instead, try using something like this:
-(IBAction)buttonPushed: (id) sender
{
NSString* s = #"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: #", "];
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 0]];
NSDecimalNumber* dec2 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 1]];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum, nil];
}
Also note that I'm splitting the string with ", " and not just " ", otherwise the first string will be "25.55,"
See the NSDecimalNumber documentation for the details: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSDecimalNumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDecimalNumber/decimalNumberWithString:

How to parse integer value to Json iOS?

I was passed NSstring it is coming properly data into uitableview but when I am parsing integer value it showing exception Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x744e9d0
This is my code
(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray * values=[json objectForKey:#"loans"];
NSLog(#"Array: %#",values);
for (NSDictionary *file in values)
{
NSNumber *fileTitle=[file objectForKey:#"id"];
// NSString *fileTitle = [file objectForKey:#"sector"];
[titles addObject: fileTitle];
}
[self.mainTableView reloadData];
}
You are assigning string value to NSNumber type.
Try to use this
NSString *fileTitle=[file objectForKey:#"id"];
[titles addObject: fileTitle];
and use string value.
[[titles addObject: fileTitle] stringValue];
I guess you are doing something like this in your -tableView: cellForIndexPath:
UITableViewCell *cell = [...];
cell.titleLabel.text = titles[indexPath.row];
You can only use NSString for -[UILabel setText:] method.
Convert your NSNumber to NSString with a simple -[NSNumber stringValue] or using NSNumberFormatter.
Everything seems ok. Just replace
NSNumber *fileTitle=[file objectForKey:#"id"];
by NSString *fileTitle = [file objectForKey:#"id"];

Resources