在'id'类型的对象上找不到属性“ [英] Property '' not found on object of type 'id'

查看:1196
本文介绍了在'id'类型的对象上找不到属性“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试读取或写入变量到数组时,得到 属性'aVariable'在类型ID 不应该知道我添加的对象是什么类?还注意到它工作原理读取的值与 NSLog(@%@,[[anArray objectAtIndex:0] aVariable]);



我是Objective C的初学者,所以这可能是我没有得到的一些简单的东西。



AnObject



  @interface AnObject:NSObject 

@property(nonatomic,readwrite)int aVariable;

@end



AnotherObject



  @interface AnotherObject:NSObject 

@end



test.h



  #importtest.h

@implementation AnObject
@synthesize aVariable;

- (id)init
{
self = [super init];
if(self){
aVariable = 0;
}
return self;
}

@end



test.m



  @implementation AnotherObject 

- (id)init
{
self = [super在里面];
if(self){}
return self;
}

- (NSMutableArray *)addToArray
{
NSMutableArray * anArray = [[NSMutableArray alloc] initWithCapacity:0];
AnObject * tempObject = [[AnObject alloc] init];

tempObject.aVariable = 10;
[anArray addObject:tempObject];

//类型'id'的对象上没有找到属性'aVariable'
[anArray objectAtIndex:0] .aVariable = [anArray objectAtIndex:0] .aVariable + 1;

//类型'id'的对象上找不到属性'aVariable'
NSLog(@%i,[anArray objectAtIndex:0] .aVariable);

//这个工作
NSLog(@%i,[[anArray objectAtIndex:0] aVariable]);

return anArray;
}

@end


解决方案>

此代码:

  [anArray objectAtIndex:0] .aVariable 

可分为两部分:

  [anArray objectAtIndex:0] 

这会返回 id - 因为你可以把任何类型的对象放入数组。编译器不知道该方法将返回什么类型。

  .aVariable 

这是要求从数组返回的对象的属性 aVariable - 如上所述,编译器不知道这个对象 - 它肯定赢假设它是一个 AnObject ,只是因为这是你先前添加了一两行。它必须自己评估每个语句。因此编译器会给你错误。



使用访问器方法时更容易一些:

  [[anArray objectAtIndex:0] aVariable]; 

这将给你一个警告(对象可能没有响应选择器),但它仍然让你运行代码,幸运的是你的对象 响应该选择器,所以你不会崩溃。但是这不是一个安全的依赖。编译器警告是你的朋友。



如果你想使用点符号,你需要告诉编译器从数组返回什么类型的对象。这称为投射。您可以通过两个步骤执行此操作:

  AnObject * returnedObject = [anArray objectAtIndex:0]; 
int value = returnedObject.aVariable;

或有一串括号:

  int value =((AnObject *)[anArray objectAtIndex:0])aVariable; 

需要额外的括号,以便在投射时使用点符号。如果你想使用访问器方法,你需要更少的圆括号,但是更多的方括号:

  int value = [ )[anArray objectAtIndex:0] aVariable]; 


I'm getting Property 'aVariable' not found on object of type id when trying to read or write aVariable to the array. Shouldn't it be known what class the object is that I added? Also noticed that it works to read the value with NSLog(@" %@",[[anArray objectAtIndex:0] aVariable]);

I'm a beginner at Objective C so it might be some simple thing I'm not getting.

AnObject

@interface AnObject : NSObject

@property (nonatomic,readwrite) int aVariable;

@end

AnotherObject

@interface AnotherObject : NSObject

@end

test.h

#import "test.h"

@implementation AnObject
@synthesize aVariable;

- (id)init
{
    self = [super init];
    if (self) {
        aVariable=0;
    }
    return self;
}  

@end

test.m

@implementation AnotherObject

- (id)init
{
    self = [super init];
    if (self) { }
    return self;
}  

- (NSMutableArray*) addToArray
{
    NSMutableArray* anArray = [[NSMutableArray alloc] initWithCapacity:0];
    AnObject* tempObject = [[AnObject alloc] init];

    tempObject.aVariable=10;
    [anArray addObject:tempObject];

    // Property 'aVariable' not found on object of type 'id'
    [anArray objectAtIndex:0].aVariable=[anArray objectAtIndex:0].aVariable + 1; 

    // Property 'aVariable' not found on object of type 'id'
    NSLog(@" %i",[anArray objectAtIndex:0].aVariable); 

    // This works
    NSLog(@" %i",[[anArray objectAtIndex:0] aVariable]); 

    return anArray;
}

@end

解决方案

This code:

[anArray objectAtIndex:0].aVariable

Can be broken down into 2 sections:

[anArray objectAtIndex:0]

This returns an id- because you can put any type of object into an array. The compiler doesn't know what type is going to be returned by this method.

.aVariable

This is asking for the property aVariable on the object returned from the array - as stated above, the compiler has no idea what this object is - it certainly won't assume that it is an AnObject, just because that is what you added a line or two earlier. It has to evaluate each statement on its own. The compiler therefore gives you the error.

It is a little more forgiving when using accessor methods:

[[anArray objectAtIndex:0] aVariable];

This will give you a warning (that the object may not respond to the selector) but it will still let you run the code, and luckily enough your object does respond to that selector, so you don't get a crash. However this is not a safe thing to rely on. Compiler warnings are your friends.

If you want to use the dot notation, you need to tell the compiler what type of object is being returned from the array. This is called casting. You can either do this in two steps:

AnObject *returnedObject = [anArray objectAtIndex:0];
int value = returnedObject.aVariable;

Or with a mess of brackets:

int value = ((AnObject*)[anArray objectAtIndex:0]).aVariable;

The extra brackets are required to allow you to use dot notation when casting. If you want to use the accessor methods, you need fewer round brackets but more square brackets:

int value = [(AnObject*)[anArray objectAtIndex:0] aVariable];

这篇关于在'id'类型的对象上找不到属性“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆