尝试从数组外的自定义对象获取属性时,Xcode 拒绝编译 [英] Xcode refuses to compile when trying to get property from custom object out of array

查看:30
本文介绍了尝试从数组外的自定义对象获取属性时,Xcode 拒绝编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近经常遇到这个问题.我有一个名为 testsNSMutableArray,它填充了我的自定义类 Test 的实例.我正在尝试使用以下方法访问属性:

I've been running into this problem a lot lately. I have an NSMutableArray called tests which is populated with instances of my custom class Test. I'm trying to access a property with the following method:

return [tests objectAtIndex:row].name;

然而,LLVM 总是抛出错误并拒绝编译:

However LLVM always throws an error and refuses to compile:

error: Semantic Issue: No member named 'name' in 'struct objc_object'

如何告诉编译器我将访问 Test 对象并让我编译该死的东西?

How do I tell the compiler that I'll be accessing a Test object and to let me compile the damned thing?

推荐答案

由于 objectAtIndex: 返回 id,即一个泛型指针,编译器不可能知道用什么方法名称代替您的属性访问 .name.

Since objectAtIndex: returns id, that is, a generic pointer, the compiler can't possibly know what method name to substitute for your property access .name.

在这种情况下,您必须向编译器完全指定您想要做什么.要么指定确切的方法名称:

You have to completely specify to the compiler what you want to do in this case. Either specify the exact method name:

[[tests objectAtIndex:row] name];

或指定对象的实际类型:

or specify the actual type of the object:

((Test *)[tests objectAtIndex:row]).name;

当你编写foo.bar时,编译器必须在foo的类中查找与属性bar相关联的方法名称>.那些通常是 bar/setBar:,但它们可以是任何东西;* 因为这种歧义,编译器需要知道 foo.仅给定一个通用指针,它无法找到该信息.

When you write foo.bar, the compiler has to look in foo's class and find the method names associated with the property bar. Those are usually bar/setBar:, but they could be anything;* because of that ambiguity, the compiler needs to know the actual class of foo. Given only a generic pointer, it can't find that information.

警告表明编译器还检查了.左侧的对象是否是一个带有名为name的字段的结构体.没有这样的字段,所以编译器不知道你想要它做什么.

The warning indicates that the compiler also checked whether the object on the left side of the . is a struct with a field called name. There isn't such a field, so the compiler does not know what you want it to do.

*例如,@property(retain, setter=putThisValueIntoBar, getter=hamAndEggs) Bar * bar;

这篇关于尝试从数组外的自定义对象获取属性时,Xcode 拒绝编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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