为什么子类@property没有相应的ivar隐藏超类ivars? [英] Why does a subclass @property with no corresponding ivar hide superclass ivars?

查看:128
本文介绍了为什么子类@property没有相应的ivar隐藏超类ivars?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下似乎很简单。有一个带有ivar的超类,以及一个访问(@protected)超类ivar的子类:

The following seems simple enough. There's a superclass with an ivar, and a subclass which accesses the (@protected) superclasses ivar:

// Testclass.h    
@interface TestClass : NSObject {
    NSString *testIvar;
}
@end

//TestClass.m
@implementation TestClass
@end

//TestSubclass.h
@interface TestSubClass : TestClass {
}

@property (nonatomic, retain) NSString *testProperty;
- (void) testMethod;

@end

//TestSubclass.m    
#import "TestSubClass.h"
@implementation TestSubClass

@synthesize testProperty;

- (void) testMethod{
    NSLog(@"The value was: %@", testIvar);
}
@end

简单而正确 - 看似足够。但是,尝试编译(对于iOS 4.2 SDK,使用GCC 4.2)会产生指向NSLog行的错误:'testIvar unclared'。

Simple and correct-seeming enough. However, attempting to compile (for iOS 4.2 SDK, with GCC 4.2) produces this error pointing to the NSLog line: 'testIvar undeclared'.

我是Objective的新手-C,但不能为我的生活看到为什么这应该是一个错误。注释掉testProperty的东西,它编译好了。看起来在子类中添加一个合成属性而没有相应的ivar,实际上是隐藏了一个不相关的超类ivar。

I'm new to Objective-C, but can't for the life of me see why this should be an error. Comment out the testProperty stuff, and it compiles OK. It seems like adding a synthesized property in a subclass, without a corresponding ivar, is actually hiding an unrelated superclass ivar.

任何人都可以告诉我这里发生了什么?相关的,编译错误是否可以预见? (预见它会节省我一些时间和挫折感。)

Can anyone enlighten me as to what's happening here? Relatedly, was the compilation error foreseeable? (Foreseeing it would have saved me some time and frustration).

推荐答案

LLVM编译源代码而无怨言,切换到LLVM:选择target→获取信息→构建→C / C ++编译器版本→LLVM 1.5。从我有限的经验来看,无论如何它都是一个更好的编译器。不知道GCC为什么会这样做 - 有趣的捕获。

LLVM compiles the source without complaints, switch to LLVM: Select target → Get Info → Build → C/C++ Compiler Version → LLVM 1.5. From my limited experience it’s a better compiler anyway. No idea why GCC behaves the way it does – interesting catch.

这篇关于为什么子类@property没有相应的ivar隐藏超类ivars?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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