gpt4 book ai didi

ios - 托管对象方法错误 "unrecognized selector sent to instance"

转载 作者:行者123 更新时间:2023-11-29 05:00:09 25 4
gpt4 key购买 nike

在自学 Objective C 和 iOS SDK 的过程中。我已经到了这样一个地步:我认为我明白自己在做什么,但我却遇到了一个看不到出路的障碍。我创建了一个包含多个实体的核心数据模型,其中一个主实体与其他几个实体具有多对一的关系。我能够很好地处理主要实体,以及与主要实体存在一对一关系的任何对象。但是,当我尝试使用核心数据生成的访问器将实体添加到基于 NS Set 的实体之一时,我收到无法识别的选择器错误:这里有一些代码使事情更加困惑:

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

运行此代码时,出现以下错误:

2011-08-24 01:49:52.643 DigitalRecordSheet[12947:f203] -[Mech insertObject:inWeaponListAtIndex:]: unrecognized selector sent to instance 0x718bbe0

现在,inputMech 是我之前提到的主实体类型。这是从 Mech.h 生成的核心数据选择器:

(void)insertObject:(Equipment *)value inEquipmentListAtIndex:(NSUInteger)idx;

据我所知,我正在正确发送消息,但它不起作用。基本上,我想要一个拥有多种武器列表的机甲。现在,我的做法错了吗?我假设我必须首先创建一个新的武器对象,按照我想要的方式设置它,然后将其添加到 Mech 的 WeaponList NSSet 对象中。但核心数据选择器不起作用,所以我不得不假设我做错了什么。这里的任何建议将不胜感激,因为我已经翻阅了多本书和指南,但没有一本真正深入探讨处理这些类型的关系的过程......提前感谢您的任何建议。干杯,

J^2

最佳答案

此错误不是由您的数据模型配置引起的。 :

unrecognized selector sent to instance

…错误总是由于某种原因在变量中使用错误的类而导致。

在本例中,inputMech 变量中的类 Mech 没有:

insertObject:inEquipmentListAtIndex:

...方法。

最可能导致错误的原因是,在编写所有这些内容的过程中,您创建了 Mech 类文件的多个副本。一个配置为有序关系,而另一个则没有。您实际上是在使用没有的编译器进行编译。

在第二个问题中,您只需将 NSSet 对象发送到需要 Weapon 对象的方法参数。您将 addWeaponListObjectaddWeaponListObjects:

混淆了

关于ios - 托管对象方法错误 "unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7172949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com