How to set the value of UILabel in iphone? - uilabel

I'm doing this to set the value in UILabel:
NSMutableDictionary *responseDict = [responseString objectFromJSONString];
self.pkrLbl.text= [responseDict objectForKey:#"amount"];
and I'm getting this error:
[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9440310
2013-05-16 15:23:34.281 EasyLoadPakistan[20341:19a03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9440310'
Please help me how can I set value in UIlabel

A UILabel expects a string (specifically NSString or one of its subclasses), and you are passing it a number (an NSNumber instance).
Convert the number to a string first, then set the label to that string:
NSNumber *n = [responseDict objectForKey:#"amount"];
NSString *stringVersion = [n stringValue];
self.pkrLbl.text = stringVersion;
If you prefer to have it all on one line, you can omit the variables and just chain the stringValue call. The approach shown above tends to facilitate debugging, though (you can for instance set a breakpoint and have a look at your variables).

Related

Error trying to separate an array containing managed object context

Could anyone please tell me why I am getting this error and why this code isn't working?
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSArrayI componentsSeparatedByString:]: unrecognized
selector sent to instance 0x109494750'
This is the code with problems:
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:nil];
NSString *dateString = [array valueForKey:#"dateString"];
NSArray *datesArray = [dateString componentsSeparatedByString:#","];//line with problems
When you call valueForKey: on array the result will be an NSArray containing the result of calling valueForKey: on each of it's elements.
So dateString is not actually a string it's an instance of NSArray, which does not respond to componentsSeparatedByString:. You need to index into the array to get the date you want before calling componentsSeparatedByString: on that
It's an indication that dateString isn't a string. When you call -valueForKey: on an array, it returns an array. Per the docs:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
So you're calling a string method on an array. It's not clear what you're trying to accomplish by calling -valueForKey:. Perhaps you meant -objectAtIndex:?

Array cannot be formed from single element in NSMutableDictionary -[__NSCFString count]:

I'm setting objects to my NSMutableDictionary like this
if ([detail isEqualToString:[check objectAtIndex:1]]) {//check is NSArray,getdict is NSMutableDictionary,keyverify is NSMutableArray
if ([getdict objectForKey:detail]==nil) {
[getdict setObject:[check objectAtIndex:2] forKey:detail];
[keyverify addObject:[check objectAtIndex:2]];
}
else{
[keyverify addObject:[check objectAtIndex:2]];
[getdict setObject:keyverify forKey:detail];
}
}
NSArray * passarray=[getdict valueForKey:detail];
when my NSMutableDictionary value is like this
dict:{
"Sample" = (
"Test1",
Test2,
Test3
);
}
i can get an array from NSMutableDictionary and displayed in UITableView but when i get my NSMutableDictionary value like this
dict:{
"Sample" = Test1;
}
my app crashes with an log
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x6eab4c0'
Kindly help me pls..Thanks..
In your second test case the key "Sample" is not an array that is why you get that error.
You can check this by using isKindOfClass : [NSArray class], if it passes the condition then it is an array and you can do the array methods you want to if it is not then get the key "Sample" as a string.

Trying to remove a decimal from NSString

This is bizarre.
Developing for the iPhone I have an NSString:
distanceFromTargetString = #"6178266.000000"
// this is not actually code, but a value taken from elsewhere. It is a proper NSString though.
When I use this
NSArray *listItems = [distanceFromTargetString componentsSeparatedByString:#"."];
distanceFromTargetString = [listItems objectAtIndex:0];
or this
[distanceFromTarget setText: distanceFromTargetString];
I get something like this
-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40
2011-07-21 14:29:24.226 AssassinBeta[7230:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40'
Any ideas?
At some point you are assigning an NSDecimalNumber to distanceFromTargetString rather than an NSString. There is no run-time type checking of assignments in Objective C, so this is totally "legal":
NSDecimalNumber *number = [NSDecimalNumber ....];
[array addObject:number];
NSString *string = [array lastObject];
The above will generate no errors or warnings until you try to send NSString methods to string, at which point you will get an exception (crash) like you show above.
Do an audit of everywhere you assign distanceFromTargetString, and everywhere you use NSDecimalNumber. Somewhere you're crossing the streams.
You could try :
NSInteger i = [distanceFromTargetString integerValue];
NSString s = [NSString stringWithFormat:#"%d", i];
// you got your string
You are somewhere calling isEqualToString with a NSDecimalNumber as the receiver. What is distanceFromTarget? Is this an NSDecimalNumber?
The first thing should work.
You could try to set a breakpoint at the line
[distanceFromTarget setText: distanceFromTargetString];
and see if distanceFromTargetString is actually an NSString.
As mentioned above, somehow an NSNumber has sneaked in somewhere.

Why am I getting this: _cfurl: unrecognized selector

My init starts like this:
- (id) init {
[super init];
sounds = makeDictFromArrayOfURLs(getNoiseFileURLs());
[sounds retain];
NSURL *theFirstNoise = [[sounds allKeys] objectAtIndex:0];
CFURLRef uref = (CFURLRef)theFirstNoise;
OSStatus ret = AudioServicesCreateSystemSoundID(uref, &chosenNoise);
When we get to that last line, it throws this:
2011-06-09 23:19:18.744 SuperTimer[94516:207] -[NSPathStore2 _cfurl]: unrecognized selector sent to instance 0x940cfb0
2011-06-09 23:19:18.746 SuperTimer[94516:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSPathStore2 _cfurl]: unrecognized selector sent to instance 0x940cfb0'
Yeah, it's a bit uncompact for debugging.
Just before I get the dump, theFirstNoise contains the expected (sort of) data. (It's description method prints a weird form, but I am informed that's normal.)
Off the top of my head, it looks like theFirstNoise is actually an NSPathStore2 (a private subclass of NSString) instead of an NSURL.
Edit: NSPathStore2 objects will contain file paths. If you need to turn these into NSURLs, you can simply pass them to +[NSURL fileURLWithPath:].
This line:
NSURL *theFirstNoise = [[sounds allKeys] objectAtIndex:0];
is the problem: [sounds allKeys] returns an NSArray of keys, and objectAtIndex: therefore is returning an NSString, and not the URL. I wish the compiler would have been a little more helpful.

Unrecognized Selector Sent to Instance [NSCFString subarrayWithRange:]

I have the following code which is producing this error. I cannot understand why the subarrayWithRange message is being sent to a string? When it is clearly an array?
static const int kItemsPerView = 20;
NSRange rangeForView = NSMakeRange( page * kItemsPerView, kItemsPerView );
NSMutableArray *temp = [[APP_DELEGATE keysArray] mutableCopyWithZone:NULL];
NSArray *itemsForView = [temp subarrayWithRange:rangeForView];
for (int loopCounter = 0;loopCounter < r*c;loopCounter++){
NSLog(#"%i: %# ", loopCounter, [itemsForView objectAtIndex:loopCounter]);
}
Error:
-[NSCFString subarrayWithRange:]: unrecognized selector sent to instance 0x6b071a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [NSCFString subarrayWithRange:]:
Thanks
These kinds of errors are usually memory-management-related. Essentially, you're sending a message to an address that's now occupied by some other object because the previous occupant has unexpectedly disappeared. Since that address space could be occupied by anything, you just happen to be asking an NSCFString something to which it doesn't respond.
If you pause the debugger right after you create the temp array, what do you see assigned to temp? I'm guessing something's not quite right with whatever -keysArray returns. You might want to double-check how the memory is handled in whatever that's supposed to return. By the name, I suppose your app delegate has an array called "keysArray" as an instance variable. Perhaps that's not being properly retained when it's created or assigned?
So I had this one. I did something stupid. I assigned the UITextView to a string instead of it's text property. ie:
myObj.txtbxThing = [NSString stringWithFormat:#"%#", stuffString];
instead of:
myObj.txtbxThing.text = [NSString stringWithFormat:#"%#", stuffString];