简体   繁体   English

iOS中iVars的只读属性存在奇怪问题

[英]Strange issue with readonly properties for iVars in iOS

Here is what I have: 这是我有的:

@interface engine : NSObject
{

    int test;

}

@property (nonatomic, readonly) int test;

When I try to assign a value(10) to iVar test within my own class (engine class), the debugger shows test = 10, and _test = 0. 当我尝试在我自己的类(引擎类)中为iVar测试分配值(10)时,调试器显示test = 10和_test = 0。

When I try to read from test, I get 0, I'm guessing i'm getting the value of_test. 当我尝试从测试中读取时,我得到0,我猜我得到了值of_test。

How do I make it work the way it is supposed to? 如何让它按照预期的方式工作? I want outside classes to have readonly access to "test", but within my class, I can assign and read values to/from "test"? 我希望外部类只能读取“test”,但在我的课程中,我可以为“test”分配和读取值吗?

Or what is wrong with this implementation? 或者这个实现有什么问题?

UPDATE:I do no syntesize as I use xcode 4.4 which automatially does it for you. 更新:我没有同步,因为我使用xcode 4.4,它自动为你做。 I assume the synthesize will use the same iVar name as the property. 我假设合成将使用相同的iVar名称作为属性。 Which is the default behaviour anyways if you do a normal synthesize. 如果进行正常的合成,这是默认行为。

Thanks. 谢谢。

It is hard to say without seeing your code, but I am guessing, that you are trying to do something like test=10 instead of self.test=10 . 很难说没有看到你的代码,但我猜,你正在尝试做一些像test=10而不是self.test=10 Depending on the version of ObjC/XCode you use and on a possible @synthesize statement, you are accessing a (whereever defined) local variable, but not the property resp. 根据您使用的ObjC / XCode的版本以及可能的@synthesize语句,您正在访问(定义的)局部变量,但不访问属性resp。 it's iVar. 这是iVar。

EDIT 编辑

When you want to write a readonly property, you should use its iVar. 如果要编写只读属性,则应使用其iVar。 Eg, after _test=10 both read accesses, self.test and _test (but not pure test !) should provide the same value. 例如,后_test=10既读访问, self.test_test (但不是纯test !)应该提供相同的值。

You assume wrong. 你认为是错的。 The default implementation is to produce the property name with an under bar. 默认实现是使用下划线生成属性名称。 If you want a different name you will need to synthesize it yourself. 如果你想要一个不同的名字,你需要自己合成它。

Automatic synthesize creates an instance variable with a underscore prefix, so a property called test generates a backing instance variable of _test . 自动合成创建带有下划线前缀的实例变量,因此名为test的属性会生成_test的后备实例变量。 So you have two instance variables; 所以你有两个实例变量; test , which isn't connected to the property, and _test . test ,没有连接到属性, _test If you write to the instance variable test , it will not be reflected in _test or the property's value. 如果您写入实例变量test ,它将不会反映在_test或属性的值中。

But more to the point, declaring the backing instance variable yourself is pointless. 但更重要的是,自己声明支持实例变量毫无意义。 It's just an extra code and unnecessary redundancy. 这只是一个额外的代码和不必要的冗余。 The @property line already contains type, name, whether it's read/write or read-only and a storage qualifier. @property行已包含类型,名称,是读/写还是只读以及存储限定符。 Just skip declaring the backing instance variable yourself and use the one the compiler generates for you. 只需跳过自己声明支持实例变量并使用编译器为您生成的变量。

In Engine.h: 在Engine.h中:

@interface Engine : NSObject
@property (readonly, assign) NSInteger test;
- (void)doSomething;
@end

In Engine.m: 在Engine.m中:

@implementation Engine

- (void)doSomething {
    _test++; // _test is created automatically by the compiler
}

@end

In other.m: 在other.m:

Engine *engine = [[Engine alloc] init];
NSLog(@"%d", engine.test); // 0
[engine doSomething];
NSLog(@"%d", engine.test); // 1

This all just works. 一切正常。 Don't add to it. 不要添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM