- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
编辑: 我刚刚注意到另一个 Stack Overflow 问题问了同样的事情:Why does a subclass @property with no corresponding ivar hide superclass ivars?
这是一些有趣的行为,我无法在任何官方或非官方(博客、推文、SO 问题等)中找到记录。我已经将它归结为它的本质,并在一个新的 Xcode 项目中对其进行了测试,但我无法解释它。
MyBaseClass 有一个实例变量:
@interface MyBaseClass : NSObject {
NSObject *fooInstanceVar;
}
@end
MySubclass 扩展了 MyBaseClass,并声明了一个完全不相关的属性(即,该属性不打算由实例变量支持):
#import "MyBaseClass.h"
@interface MySubclass : MyBaseClass { }
@property (nonatomic, retain) NSObject *barProperty;
@end
如果 MySubclass 的实现不合成属性但实现访问器方法,则一切都很好(没有编译器错误):
#import "MySubclass.h"
@implementation MySubclass
- (NSObject*)barProperty {
return [[NSObject alloc] init]; // pls ignore flagrant violation of memory rules.
}
- (void)setBarProperty:(NSObject *)obj { /* no-op */ }
- (void)doSomethingWithProperty {
NSArray *array = [NSArray arrayWithObjects:self.barProperty, fooInstanceVar, nil];
NSLog(@"%@", array);
}
@end
但如果我删除属性访问器方法并将它们替换为属性的 synthesize
声明,我会收到编译器错误:'fooInstanceVar' undeclared (first use in this function)
.
#import "MySubclass.h"
@implementation MySubclass
@synthesize barProperty;
- (void)doSomethingWithProperty {
NSArray *array = [NSArray arrayWithObjects:self.barProperty, fooInstanceVar, nil];
NSLog(@"%@", array);
}
@end
如果我删除 synthesize
声明,或者如果我不从 MySubclass.m 中引用 fooInstanceVar
实例变量,或者如果我将所有接口(interface)和实现定义在一个文件中。这个错误似乎也发生在 GCC 4.2 和 GCC/LLVM build设置中。
谁能解释一下这里发生了什么?
最佳答案
正如在这个问题中的回答:objective c xcode 4.0.2: subclass can't access superclass variables "was not declared in this scope"
来自文档:Apple Objective-C Programming Langage: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF125
The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.
However, a public instance variable can be accessed anywhere as if it were a field in a C structure. For example:
Worker *ceo = [[Worker alloc] init];
ceo->boss = nil;
我在使用 LLVM GCC 4.2 时遇到编译错误(对于 iOS 项目,在设备上):
error: 'fooInstanceVar' undeclared (first use in this function)and the same one using GCC 4.2 :error: 'fooInstanceVar' undeclared (first use in this function)
我可以使用 LLVM Compiler 2.0 编译而不会出错。
对于使用 self-> 的 LLVM GCC 4.2 和 GCC 4.2 进行编译:
[NSArray arrayWithObjects:self.barProperty, self->fooInstanceVar, nil];in the doSomethingWithProperty method.
关于objective-c - 如果子类引用父类(super class) ivar,则合成不相关的属性会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822518/
问题比较Java,但是我想在Android中实现: 假设有 3 个或更多类相互扩展: class A { ... int color; ... } class B extend
我知道标题听起来有点奇怪,但这正是我愿意做的。简单解释:A类是B类的子类,B类也是C类的子类>. 现在,所有这些类都包含方法m()。在我的 A 类中,这是我唯一可以访问的类,因为其他类仅在运行时可用,
我有一个 UIViewController 类 A 和 B。 A 使用以下方式加载 B:[A.view addSubView B.view]。 B 有一个带有“后退”按钮的导航栏。我想在单击时返回到
我有以下(第三方)类结构。我们将调用第三方项目 ProjectSeriously,并注意我使用 System.out.println 代替其他复杂的功能(100 行代码) . class A {
在下面的代码中,我从 Game 扩展了 MyGame。我有两个问题: 我们是否需要为所有render()、dispose()、pause()调用super方法 和 resize(w,h)?很多人都没有
例如,假设我想在调用 super.viewDidLoad() 时跳过一级。所以我希望能够做这样的事情: override func viewDidLoad() { super.super.vi
public class Faculty extends Employee { public static void main(String[] args) { new Fac
假设我有: class Superclass { //fields... methodA() {...} methodB() {...} ... } class Subclass exte
这个问题在这里已经有了答案: Why is super.super.method(); not allowed in Java? (22 个答案) 关闭 9 年前。 我怀疑我想做的事情是否可行。我有
我有一个实现 Initializable 的类。 public abstract class ExampleClass implements Initializable { public vo
我想知道,我有这个大数组,是否可以只在内存中使用一次而不是每个线程一次?以 stackoverflow 上的标签为例。他们几乎从不改变,为什么不为他们留下一个内存点呢?甚至可能将该数组永久保存在内存中
假设这三个类具有这个简单的层次结构: class A { func foo() { print("A") } } class B: A { override fu
有没有办法在 TypeScript 中调用 super.super.methodName。我想避免调用super.methodName,但我想调用二祖的methodName方法。 谢谢。 最佳答案 T
这个问题已经有答案了: When do I use super()? (11 个回答) 已关闭 7 年前。 package Geometry; public abstract class Geomet
我必须执行and()在我的实现 Predicate 的业务对象上. 出现问题的代码是 and() 行调用: Predicate predicate = new M
我有一个实现接口(interface)的抽象父类(super class): public abstract class FooMatrix implements Matrix { publi
我有四个 UIView:viewA 是 Root View ,它有 viewB 作为它的 subview 。 viewB 将 viewC 作为其 subview ,而 viewC 将 viewD 作为
有什么区别: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() 和:
我有一个通用接口(interface) interface ListList extends List> .由于某些原因,我无法转换 ListList至 List> .有什么方法可以做到吗?为什么它不
我想调用带有两个参数的父类(super class)的构造函数,所以我调用了 super(arguments),但是编译器说: “类 Person 中的构造函数 Person 不能应用于给定类型; 要
我是一名优秀的程序员,十分优秀!