ios – 为什么我必须在Header文件中定义两次变量?

ios – 为什么我必须在Header文件中定义两次变量?,第1张

概述为什么我必须在头文件中定义两次变量?这些变量之间有什么不同? 第一个定义是: @interface MyController: UIViewController{ NSInteger selectedIndex;} 第二个定义是: @property (nonatomic) NSInteger selectedIndex; What you’re seeing was required 为什么我必须在头文件中定义两次变量?这些变量之间有什么不同?

第一个定义是:

@interface MyController: UIVIEwController{    NSInteger selectedindex;}

第二个定义是:

@property (nonatomic) NSInteger selectedindex;
解决方法@H_301_24@

What you’re seeing was required in earlIEr versions of Objective-C,
but isn’t any more.

In the first versions of Objective-C used by NeXT up until the new
runtime was introduced (with Objective-C 2.0 on Mac OS X),all
instance variables had to be declared as part of the class’s structure
in its @interface. The reason was that if you subclassed a class,
the compiler needed to kNow the instance variable layout of the class
so it Could see at what offset to put the subclass’s instance
variables.

When propertIEs were introduced,synthesized propertIEs had to be
“backed” by an instance variable in the class’s structure. Therefore
you had to declare both an instance variable and the property.

All of the above is no longer true. Newer Objective-C is less fragile
in the way it looks up instance variable offsets,which has meant a
few changes:

not all instance variables need to be in the @interface. They can Now be defined in the @implementation: though not in categorIEs due
to the possibilitIEs of clashing and other issues. instance variables for synthesized propertIEs can be inferred and created based on the property deFinition. you can programmatically add instance variables to classes you’re creating at runtime (only before you’ve registered the class as
available to the system).

So,to reiterate,you only needed to declare both the instance
variable and a synthesized property in older versions of the
Objective-C language. What you’re seeing is redundant and should not
be consIDered a “best practice”.

[Source]

总结

以上是内存溢出为你收集整理的ios – 为什么我必须在Header文件中定义两次变量?全部内容,希望文章能够帮你解决ios – 为什么我必须在Header文件中定义两次变量?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1006267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存