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

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

问题描述

我在尝试读取或写入数组的 aVariable 时,在类型为 id 的对象上找不到 Property 'aVariable'.难道不应该知道我添加的对象是什么类吗?还注意到它可以使用 NSLog(@" %@",[[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]);

我是 Objective C 的初学者,所以这可能是我不明白的一些简单的事情.

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

@interface AnObject : NSObject

@property (nonatomic,readwrite) int aVariable;

@end

另一个对象

@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

推荐答案

这段代码:

[anArray objectAtIndex:0].aVariable

可以分为两个部分:

[anArray objectAtIndex:0]

这将返回一个 id - 因为您可以将任何类型的对象放入数组中.编译器不知道此方法将返回什么类型.

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

这是要求从数组返回的对象上的属性 aVariable - 如上所述,编译器不知道这个对象是什么 - 它当然不会假设它是一个 AnObject,只是因为这是您之前添加的一两行.它必须自行评估每个语句.因此编译器会给你错误.

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;

或者用一堆括号:

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天全站免登陆