Unrecognized Selector Sent to Instance [NSCFString subarrayWithRange:] - objective-c

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

Related

I want to coordinate two NSMutableDictionary(s)

dictProductList = (
{
"product_capacity" = "20.00";
"product_code" = ITEM001;
"product_description" = test;
"product_id" = 1;
"product_name" = "Water Bottle";
"product_price" = "25.00";
"product_unit" = LTR;
}
)
and another one is
NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
[firstOne setObject:#"5" forKey:#"product_quantity"];
While I am try to add this two mutable dictionary I got error. Am using this below method.
[dictProductList addEntriesFromDictionary:firstOne];
The error message is
-[__NSArrayM addEntriesFromDictionary:]: unrecognized selector sent to instance 0x166d87f0
Show the code that creates dictProductList. It looks like it is an array with one dictionary in it.
The error: " -[__NSArrayM addEntriesFromDictionary:]: unrecognized selector sent to instance 0x166d87f0"
says that the message addEntriesFromDictionary was sent to an NSMutableArray.
It in fact dictProductList is an NSMutableArray the correct code would be:
[dictProductList addObject:firstOne];
In Objective-C the error message are usually helpful and it is worthwhile taking the time to understand them. In this case it say the message was sent to an NSMutableArray and NSMutableArray has no message addEntriesFromDictionary. So either the object is incorrect or the message is incorrect.

How to set the value of UILabel in iphone?

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

nsdictionary issue

I have the following data in a NSDictionary Variable:
{
value = "TV-PG";
}
I was wondering how to get the value for key "value" here.
I tried:
NSDictionary *fieldMaturityRating = [[parsedItems objectAtIndex:0] objectForKey:#"field_maturity_rating"];
NSString *dateRelease = [fieldMaturityRating objectForKey:#"value"];
(where, fieldMaturityRating is a NSDictionary with the given value)
and I get:
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xd9cd3f0
[10530:707] *** Terminating app due to uncaught exception: -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xd9cd3f0
Can anyone kindly help me ?
Thanks.
Note: if I pause the execution and do a po after the 1st line of code presented here, I get the following:
(gdb) po fieldMaturityRatingNew
<__NSArrayM 0x79af250>(
{
value = "TV-PG";
}
)
The po actually shows your issue:
(gdb) po fieldMaturityRatingNew
<__NSArrayM 0x79af250>(
{
value = "TV-PG";
}
)
The outer ( and ) mean that your object is actually an array.
Inside that is where the { and } denote your dictionary.
So you really want:
NSString *value = [[fieldMaturityRatingNew objectAtIndex:0] objectForKey:#"value"];
You're actually sending that NSDictionary message to a NSMutableArray instance.
You might want to check your code again as the objectForKey: method is right when pointing to a NSDictionary.
This means your fieldMaturityRating is not actually an NSDictionary. Make sure you aren't setting it to an array somewhere in your code.
Edit:
This means your fieldMaturityRating is actually an NSArray containing an NSDictionary. If this is your intended data structure then you can access your value like so.
NSString *dateRelease = [[fieldMaturityRating objectAtIndex:0] objectForKey:#"value"];
I don't believe this is your intended data structure so you should look into why your parsedItems array returned you an NSArray instead of an NSDictionary. If you track this problem down you can stop any headaches in the future.
Based on your datastructure which is a dictionary inside an array, dateRelease should be like this
NSString *dateRelease = fieldMaturityRating[0][#"value"];

Array not retaining properly?

I get the following error when running my App:
2011-09-02 15:38:44.157 TheApp[9973:207] -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b28990
2011-09-02 15:38:44.160 TheApp[9973:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b28990'
And Xcode marks the line in this function in green:
- (void)oneCheckAndSetStrokes {
playerOneScoreNum.text = [playerOneScore objectAtIndex:(11)]; }
Therefore I'm guessing something is messed up with the Array. After som research I came across a lot of posts like this one: NSMutableArray : unrecognized selector sent to instance which seems to inticate that the error occurs when the Array is not being retain properly (hence my title).
So I'm trying to retain the Array in the file in wich it is initiated (which by the way is not the same file as the code above. The Array is also defined in another file, Globals.h, and then imported), in the following way:
- (void)viewDidLoad {
[super viewDidLoad];
playerOneScore = [[NSMutableArray alloc] initWithCapacity:19];
[playerOneScore retain]; }
This would solve the problem according to the post refered to earlier, but in my case it does not. Has anyone encountered something similar? It seems like I'm missing something trivial here.
The error you got has nothing to do with retain, you got that error because at this line:
playerOneScoreNum.text = [playerOneScore objectAtIndex:(11)];
you are trying to set a string property using a number object! You have to use "stringValue", in this way:
playerOneScoreNum.text = [[playerOneScore objectAtIndex:11] stringValue];
ps: wrapping the index (11) with parenthesis is useless :P

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.