ios unrecognized selector [duplicate] - ios

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
-[__NSCFDictionary JSONRepresentation]: unrecognized selector sent to instance
I use SBJson ( http://stig.github.com/json-framework/ ) in two of my project. Therefore I downloaded the code and copied it into my first project so that I can do something like this
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
[dict setValue:email forKey:#"email"];
[dict setValue:password forKey:#"password"];
NSString* json = [dict JSONRepresentation];
Now for my second and new project I did the same. I copied all the source files from SBJson into my new project and used the exact same code as above. But now when the program comes to line NSString* json = [dict JSONRepresentation]; I get the following error message:
-[__NSCFDictionary JSONRepresentation]: unrecognized selector sent to instance 0x689c710
What am I doing wrong in my second project?

Make sure that all the files have been added to the target.

Since iOS 5 you don't need an external library to use JSON, This tutorial could help you with that.
That error is because you are telling to dict, which is an instance of NSMutableDictionary, to perform a method called JSONRepresentation. dict doesn't know how to do that. I haven't work with that library but I would guess that you need to make an instance of SBJSON parser and then send dict as parameter. I found this and this tutorials, I hope they can help you.

Related

-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object Return then Get from NSUserDefaults

The following code is returning an exception with the following error message and My Application Crashed, in my code all data store in NSMutableDictionary and then store in NSUserDefaults
After get the data and assign global NSMutableDictionary and i will try to update data in NSMutableDictionary app crash and showing error
-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary * cacheUploadDataDic = [[defaults objectForKey:#"SalaryIncomeData"] mutableCopy];
Exactly, it crashes with mutable type issue, all the objects you retrieved from user defaults are inmutable, it uses the plist file to serialized the data list to file, check the plist programming guide about the supported types.
You shall regard the user defaults as a simple plist file storage wrapper.
Use NSMutableDictionary's 'initWithDictionary:' method instead.
Since an answer to the question in my comment does not appear to be forthcoming, I'm just going to go out on a limb and make the guess that your cacheUploadDataDic dictionary contains some more NSDictionary objects inside of it, and that your crash is occurring when you try to mutate one of those dictionaries. This fails because mutableCopy performs a shallow copy; only the dictionary object itself is copied, and all of the objects inside the dictionary, including any additional dictionaries, remain their original immutable selves.
You can fix this by making a deep copy of the dictionary instead. There are a few ways to do this, but the simplest is probably to use the CFPropertyListCreateDeepCopy function. Unfortunately, we have to do some bridging, since this API is only available at the CoreFoundation level, the reason for which is one of those eternal mysteries.
Anyway, do something like this:
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:#"SomeKey"];
NSMutableDictionary *mutableDict = CFBridgingRelease(
CFPropertyListCreateDeepCopy(kCFAllocatorDefault,
(__bridge CFDictionaryRef)dict,
kCFPropertyListMutableContainersAndLeaves)
);
You will now be able to mutate the contents of mutableDict to your heart's content.
Follow your code, there shows no problem.
Then if possible, you can check the class of cacheUploadDataDic before update data and after NSMutableDictionary * cacheUploadDataDic = [[defaults objectForKey:#"SalaryIncomeData"] mutableCopy].
If there are one NSMutableDictionary and the other is NSDictionary then you must change cacheUploadDataDic at other line.
if you can't find out where you change the object,you can try KVO.

Couchbase iOS:How to convert a CBLDocument to NSDictionary?

What I am trying to do is convert a document to an dictionary and then iterate that dictionary to see what's inside, but I am having the following problem when trying to convert the document.
I have the following code:
CBLDocument *document = row.value;
NSDictionary *dict = document.properties;
the xCode is always complaining "[__NSCFDictionary currentRevision]: unrecognized selector sent to instance 0x90c4150" when running to the second statement.
I have also tried NSDictionary *dict = document.currentRevision.properties;
It's not working either.
Could anyone help me with that?
OK, it turns out row.value is not a document(is actually a dictionary), but nobody tells me that when I emit(#[somekey], doc); The doc (which is the value) is actually a dictionary instead of a document.
But weird though the compiler did not complain when assigning the dictionary to a CBLDocument.

exception error with json in xcode

I'm beginner in ios. I developing a call with JSON for access a data and insert after on UITableView.
In this point on my code
NSDictionary * dict = [[CJSONDeserializer deserializer ] deserialize:jsonData error:&error];
When compiling I obtained this error
2013-02-07 15:49:48.078 comercMobil[12933:c07] +[CJSONScanner scannerWithData:]: unrecognized selector sent to class 0xe7b8
2013-02-07 15:49:48.080 comercMobil[12933:c07] Exception +[CJSONScanner scannerWithData:]: unrecognized selector sent to class 0xe7b8
any suggestions? thanks for all
A couple of things:
This is not a compiler error, but rather a runtime error.
I don't think this is the line that's causing this error, since you're not calling scannerWithData here. I'd search your source for a reference to scannerWithData.
But, I agree with Sean that you should consider just using NSJSONSerialization.
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
Or, if you need support for iOS prior to version 5.0, SBJSON is very popular.

JSON: NSString stringWithContentsOfURL... with null in stream, crashes

Environment: iOS 4.3+ using Xcode 4.3+
I'm always getting an iOS/Xcode crash when I implement the following string that returns data with nulls in it:
[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
NSDictionary *myResult = (NSDictionary *)[myString JSONValue];
The result:
-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x17315e8
When the data stream contains no nulls (mostly), it works fine.
Is this a known problem?
Any remedy?
You can also fix the data coming in from the source. In almost all of my web service interaction, I use COALESCE on queries and SP's to make sure that the data coming out is always consistent.

Managed Object Method Error "unrecognized selector sent to instance"

In the process of teaching myself objective C and the iOS SDK. I've gotten to a point where I think I understand what I'm doing yet I'm hitting a roadblock that I don't see a way past. I've created a core data model with multiple entities, with one master entity having a couple many to one relationships with a couple other entities. And I'm able to work with the main entity just fine, as well as any object that are on a one to one relationship with the main entity. However, when I try to add an entity to one of the NS Set based entities using the core data generated accessors, I am getting an unrecognized selector error: Here's some code to confuse things even more:
Weapon *tempWeapon = [NSEntityDescription insertNewObjectForEntityForName:#"Weapon" inManagedObjectContext:inputContext];
NSArray *tempWeaponStats = [inputMech getMechWeaponStats:tempEquipName];
tempWeapon.weaponName = tempEquipName;
tempWeapon.weaponDisplayName = tempEquipDisplayName;
tempWeapon.weaponLocation = tempEquipLocation;
tempWeapon.weaponType = tempEquipType;
tempWeapon.weaponCritSize = tempEquipSize;
tempWeapon.weaponHeat = [[NSNumber alloc] initWithInt:[[tempWeaponStats objectAtIndex:0] intValue]];
tempWeapon.weaponDamage = [[NSNumber alloc] initWithInt:[[tempWeaponStats objectAtIndex:1] intValue]];
tempWeapon.weaponRangeMin = [[NSNumber alloc] initWithInt:[[tempWeaponStats objectAtIndex:2] intValue]];
tempWeapon.weaponRangeShort = [[NSNumber alloc] initWithInt:[[tempWeaponStats objectAtIndex:3] intValue]];
tempWeapon.weaponRangeMed = [[NSNumber alloc] initWithInt:[[tempWeaponStats objectAtIndex:4] intValue]];
tempWeapon.weaponRangeLong = [[NSNumber alloc] initWithInt:[[tempWeaponStats objectAtIndex:5] intValue]];
NSLog(#"Adding to the weapon list %#", tempWeapon.weaponName);
[inputMech insertObject:tempWeapon inWeaponListAtIndex:(NSUInteger)0];
When running this code, I get the following error:
2011-08-24 01:49:52.643 DigitalRecordSheet[12947:f203] -[Mech insertObject:inWeaponListAtIndex:]: unrecognized selector sent to instance 0x718bbe0
Now, inputMech is of the master entity type I mentioned earlier. Here's the core data generated selector from Mech.h:
(void)insertObject:(Equipment *)value inEquipmentListAtIndex:(NSUInteger)idx;
As far as I can tell, I am sending the message properly, but it doesn't work. Basically, I want to have one Mech that has a list of multiple weapons. Now, am I wrong in how I am doing this? I am assuming that I have to create a new weapon object first, set it up how I want, then add it to the Mech's weaponList NSSet object. But the core data selectors aren't working so I have to assume I am doing something wrong. Any advice here would be appreciated as I've dug through multiple books and guides and none of them really go in to depth about the process of handling these types of relationships... Thanks in advance for any advice. Cheers,
J^2
This error is not caused by the configuration of your data model. The:
unrecognized selector sent to instance
… error always results from having the wrong class in a variable for some reason.
In this case, the class Mech in the inputMech variable doesn't have a:
insertObject:inEquipmentListAtIndex:
...method.
The most likely cause of your error is that in the course of writing all this, you've created multiple copies of the Mech class files. One is configured for a ordered relationships and the other isn't. You are actually compiling with the one that does not.
In your second problem, you are simply sending a NSSet object to a method parameter that expects a Weapon object instead. You are confusing addWeaponListObject with addWeaponListObjects:
take the following steps
1) created a new version of the Core Data Model via Xcode.
2) Fix the relationship (added a new relationship between the two.enter link description here)
3) re-created the NSManagedObject subclass

Resources