我正在尝试访问另一个类中的变量,但这给了我错误
在类型“ __strong id”的对象上找不到属性“ itemType”
基本上,我以此来初始化课程
GameMsgs *warningMsg = [[GameMsgs alloc]initWithItem:@"remove_village-object-warning" andCallingMethod:self];
在GameMsgs中...
- (id)initWithItem:(NSString*)itemTypeP andCallingMethod:(id)callingMethod
{
if ((self = [super init]))
{
sharedInstance = [SKGame sharedInstance];
myCallingMethod = callingMethod;
...
但是,当我尝试访问myCallingMethod中的变量时,出现上述错误。这就是我试图访问它的方式...
Text *valueT = [[Text alloc] initWithText:[[myCallingMethod.itemType objectForKey:@"templateKingdomObject"] objectForKey:@"removeCost"] withX:70 withY:60 withSize:14 withFieldWidth:100 withFieldHeight:30 withColour:0xffffff withFont:@"MarkerFelt-Thin"];
错误仅出现在itemType的开头。
myCallingMethod是一种ID。
我认为这很明显,但是我还是Obj-c的新手。
您的变量callingMethod类型为“ id”。在Obj-C领域中,“ id”除表示基本内容的地址外,不表示其他任何内容。编译器不知道callingMethod对象的真实类型,因此假定它没有任何方法。您可以通过两种方式解决此问题:
更改方法声明以为“ callingMethod”变量合并一个实际的类
- (id)initWithItem:(NSString *)itemTypeP andCallingMethod:(YourClass *)callingMethod
或者通过在需要时将其强制转换为自己的类型。
Text *valueT = [[Text alloc] initWithText:[[((YourClass *)myCallingMethod).itemType objectForKey:@"templateKingdomObject"] objectForKey:@"removeCost"] withX:70 withY:60 withSize:14 withFieldWidth:100 withFieldHeight:30 withColour:0xffffff withFont:@"MarkerFelt-Thin"];
但这很丑。
所有这些都假设您的变量“ callingMethod”是一种类型,否则请查看协议。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句