I want to coordinate two NSMutableDictionary(s) - objective-c

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.

Related

Able to add objects to NSMutableDictionary, but trying to access objects gives 'unrecognized selector' error

I'm using ARC, and I'm almost certain this is a memory related issue.
I have several classes: DiningHall, Meal, and Station. DiningHalls store various meals using a mutable dictionary, and each meal stores various stations using another mutable dictionary. When I try to access a station from the meal's stationList property, I get the following error:
Below is the code I tested to debug: before I add a new station to the dictionary, I tried listing the dictionary's existing contents, giving me the following error:
'NSInvalidArgumentException', reason: '-[__NSCFString name]: unrecognized selector sent to instance
Inside DiningHall class:
- (void) addStation:(Station*)s toMeal:(NSString*) mealType {
Meal *m = [self.mealList objectForKey:mealType];
NSArray* a = [m.stationList allKeys];
for (Station* s in a)
NSLog(#"%#", s.name); //line generating error
[m addStation:s];
}
Inside Meal class:
- (void)addStation:(Station *)station {
[self.stationList setObject:station forKey:station.name];
//also tried [self.stationList setValue:station forKey:station.name];
}
In init of meal class:
self.stationList = [[NSMutableDictionary alloc] init];
The reason I think this is a problem with the dictionary is that when I replaced the stationList dictionary with a mutable array, (and adjusted the test code appropriately), it did not crash.
You error is in this line of code:
NSArray* a = [m.stationList allKeys];
for (Station* s in a)
NSLog(#"%#", s.name); //line generating error
Because the array a contain the key and the value of the dictionary. And since you set the key of the dicionary to station.name array a only contains strings and not atations.
Try:
NSArray* a = [m.stationList allValues];
for (Station* s in a)
NSLog(#"%#", s.name); //line generating error
You are setting the station.name as key:
[self.stationList setObject:station forKey:station.name];
And then you try to access the station object when iterating over all keys:
for (Station* s in a)
NSLog(#"%#", s.name);
But the keys will only give the station names. You want
NSLog(#"%#", [self.stationList objectForKey:s]);

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.

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