需要对 iOS 中的 @property/@synthesize 进行解释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13120927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:58:46  来源:igfitidea点击:

Need explanation on @property/@synthesize in iOS

iosobjective-cproperties

提问by HpTerm

Not a complete noob I am quite new to iOS programming and to Ojbective-C. I mainly come from a background of C (DSP, Microcontrollers), Delphi XE2/Pascal , Visual Basic and Java (desktop and Android apps).

不是一个完整的菜鸟我对 iOS 编程和 Ojbective-C 很陌生。我主要来自 C(DSP、微控制器)、Delphi XE2/Pascal、Visual Basic 和 Java(桌面和 Android 应用程序)的背景。

I mainly learned Cocoa with the book "Beginning iOS 5 Development", from Apress.

我主要是通过 Apress 的“Beginning iOS 5 Development”一书学习 Cocoa。

Recently I watched videos of the WWDC 2012 and went through some of their sample code, and I must say that I am confused of what is the proper way of writing my apps and more specifically with the @property/@synthesize words.

最近我观看了 WWDC 2012 的视频并浏览了他们的一些示例代码,我必须说我很困惑什么是编写我的应用程序的正确方法,更具体地说是 @property/@synthesize 词。

In the book most (not to say all) of the sample code uses to define a property as for example

书中大部分(不是全部)示例代码用于定义一个属性,例如

ViewController.h
@property (strong, nonatomic) NSMutableArray *list;

ViewController.m
@synthesize list;

then all the code access synthesize list with

然后所有的代码访问合成列表

self.list

or even simply

甚至干脆

list

Now in every WWDC code sample I read I see that programmers define a property but then, in the .m file they do things like

现在,在我阅读的每个 WWDC 代码示例中,我看到程序员定义了一个属性,但是,在 .m 文件中,他们执行了类似的操作

@synthesize list = _list;

and access sometimes

有时访问

self.list

or

或者

_list

I am confused. What is the correct practice ? As Apple programmers all use underscore I think I should do it that way but why the book did not ? Is there a difference between list and _list ? And more over, as I am in the same object why sometime use self.list and sometimes list/_list.

我很迷惑。什么是正确的做法?由于 Apple 程序员都使用下划线,我想我应该这样做,但为什么这本书没有呢?list 和 _list 之间有区别吗?更重要的是,因为我在同一个对象中,为什么有时使用 self.list 有时使用 list/_list。

Sometimes they don't use @synthesize, I assume it's when they want to re-write their own accessors and mutators (which is never my case up to now).

有时他们不使用@synthesize,我认为这是他们想要重写自己的访问器和修改器的时候(到目前为止,这从来不是我的情况)。

I have read here and there on the web but nothing was clear enough to set my mind right, so I count on StackOverflow to bring light here.

我已经在网络上到处阅读,但没有任何内容足以让我的想法正确,所以我指望 StackOverflow 在这里带来光明。

Last but not least I prefer and answer based on current iOS 6 best practice/programming technique. Useless to tell me how to do it correctly in older iOS.

最后但并非最不重要的是,我更喜欢并根据当前的 iOS 6 最佳实践/编程技术进行回答。告诉我如何在较旧的 iOS 中正确地做到这一点毫无用处。

采纳答案by Warren Burton

There is no correct way. There is only your preferred style.

没有正确的方法。只有你喜欢的风格。

The lastest compilers do an implicit synthesise on the property declaration.

最新的编译器对属性声明进行了隐式综合。

@synthesize list = _list;.

@synthesize list = _list;.

Nothing is ever written in your code. It just happens.

您的代码中从未写过任何内容。它只是发生。

However that doesnt stop you doing this explicitly.

但是,这并不能阻止您明确地执行此操作。

@synthesize list = somethingelse;

@synthesize list = somethingelse;

So when you ask for a pointer to listvia the accessor (self.list) you will get a pointer to somethingelse

因此,当您list通过访问器 ( self.list)请求指向的指针时,您将获得指向somethingelse

In most cases NSMutableArray *thing = self.listis equivalent to NSMutableArray *thing = somethingelse

在大多数情况下NSMutableArray *thing = self.list相当于NSMutableArray *thing = somethingelse

And just because Apple uses a style doesn't mean that you have to do it. Each company usually has their own coding style.

仅仅因为 Apple 使用一种风格并不意味着你必须这样做。每个公司通常都有自己的编码风格。

The main problem with using @synthesize list;is that it poses the risk that you can write either

使用的主要问题@synthesize list;是它带来了您可以编写的风险

self.list = thingor list = thing.

self.list = thinglist = thing

The former uses the sythesised setList:accessor while the latter doesn't and put the risk of related bugs in your code , though its not as bad with ARCas you dont get leaks happening for strongproperties.

前者使用合成的setList:访问器,而后者不使用并在您的代码中放置相关错误的风险,尽管它并不ARC像您不会发生strong属性泄漏那样糟糕。

What ever style you use, keep it consistent and be aware of the effects of using an ivar directly list = thingas compared to using its accessor self.list = thing

无论你使用什么风格,保持一致并注意直接使用 ivarlist = thing与使用其访问器相比的效果self.list = thing

回答by Phillip Mills

This is a language feature that has had its usage evolve rapidly over the past few years, which explains the various forms. With the most recent tools, you can choose to ignore @synthesizeand have things work reasonably.

这是一种语言特性,其用法在过去几年中迅速发展,这解释了各种形式。使用最新的工具,您可以选择忽略@synthesize并让事情合理地工作。

The default behavior in that case produces the same effect as @synthesize list = _list;.

这种情况下的默认行为产生与 相同的效果@synthesize list = _list;