关于Objective-C中的私有实例变量

时间:2022-09-07 19:34:08

In xCode 3, I defined private instance variables in a class. When I directly access the private variables in the client codes, why does the compiler just show me a warning, not an error? The code can still run. The warning says this maybe a hard error in the future. What does the "hard error" mean? Thanks.

在xCode 3中,我在一个类中定义了私有实例变量。当我直接访问客户端代码中的私有变量时,为什么编译器只显示警告,而不是错误?代码仍然可以运行。警告说这可能是未来的一个难题。 “硬错误”是什么意思?谢谢。

2 个解决方案

#1


Hard Error means that sometime in the future the compiler will behave the way you expect it to behave (i.e., it won't compile the source file when you directly access an instance variable outside the defined visibility scope).

硬错误意味着将来的某个时候编译器将按照您期望的方式运行(即,当您直接访问定义的可见范围之外的实例变量时,它不会编译源文件)。

Right now the compiler simply isn't enforcing Objective-C the visibility restrictions. The warning is there, however, to remind you that you're doing something that you shouldn't be doing and bring you attention to that in case you did it by accident.

目前,编译器根本没有强制实施Objective-C可见性限制。然而,警告是在那里提醒你,你正在做一些你不应该做的事情,如果你偶然做到这一点,请引起你的注意。

If I had to hazard a guess as to why the visibility isn't enforced, I'd say that with all the toll-free bridging stuff between the Foundation library and the CoreFoundation library, there is probably a decent amount of library code that accesses instance variables that, strictly speaking, should not be visible.

如果我不得不猜测为什么不强制执行可见性,我会说基础库和CoreFoundation库之间的所有免费桥接东西,可能有相当数量的库代码访问严格来说,实例变量不应该是可见的。

In general, it's a bad idea to directly access instance variable anyway. As long as you can use Obj-C 2.0, it's probably better to use something like properties if you're designing a pure-data model object.

一般来说,无论如何直接访问实例变量是个坏主意。只要您可以使用Obj-C 2.0,如果您正在设计纯数据模型对象,那么使用类似属性的东西可能会更好。

#2


Well, the hard error would mean that it will become an error in the future rather than a warning. As for why it's a warning now, I'm not sure. I would imagine it's for the same reason that when you try to call a method that doesn't exist on a class, that is also only a warning.

那么,硬错误意味着它将来会成为一个错误,而不是一个警告。至于为什么现在这是一个警告,我不确定。我想是因为同样的原因,当你试图调用一个类上不存在的方法时,这也只是一个警告。

#1


Hard Error means that sometime in the future the compiler will behave the way you expect it to behave (i.e., it won't compile the source file when you directly access an instance variable outside the defined visibility scope).

硬错误意味着将来的某个时候编译器将按照您期望的方式运行(即,当您直接访问定义的可见范围之外的实例变量时,它不会编译源文件)。

Right now the compiler simply isn't enforcing Objective-C the visibility restrictions. The warning is there, however, to remind you that you're doing something that you shouldn't be doing and bring you attention to that in case you did it by accident.

目前,编译器根本没有强制实施Objective-C可见性限制。然而,警告是在那里提醒你,你正在做一些你不应该做的事情,如果你偶然做到这一点,请引起你的注意。

If I had to hazard a guess as to why the visibility isn't enforced, I'd say that with all the toll-free bridging stuff between the Foundation library and the CoreFoundation library, there is probably a decent amount of library code that accesses instance variables that, strictly speaking, should not be visible.

如果我不得不猜测为什么不强制执行可见性,我会说基础库和CoreFoundation库之间的所有免费桥接东西,可能有相当数量的库代码访问严格来说,实例变量不应该是可见的。

In general, it's a bad idea to directly access instance variable anyway. As long as you can use Obj-C 2.0, it's probably better to use something like properties if you're designing a pure-data model object.

一般来说,无论如何直接访问实例变量是个坏主意。只要您可以使用Obj-C 2.0,如果您正在设计纯数据模型对象,那么使用类似属性的东西可能会更好。

#2


Well, the hard error would mean that it will become an error in the future rather than a warning. As for why it's a warning now, I'm not sure. I would imagine it's for the same reason that when you try to call a method that doesn't exist on a class, that is also only a warning.

那么,硬错误意味着它将来会成为一个错误,而不是一个警告。至于为什么现在这是一个警告,我不确定。我想是因为同样的原因,当你试图调用一个类上不存在的方法时,这也只是一个警告。