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 wo开发者_高级运维rk. 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
精彩评论