开发者

When is @property and @synthesize needed?

开发者 https://www.devze.com 2023-02-13 16:46 出处:网络
When exactly do I have to add @property (nonatomic, retain) and @synthesize? Also, when is declaring IBOutlet someObject enough? How is it that I can set/get UILabel value without @proper开发者_开发问

When exactly do I have to add @property (nonatomic, retain) and @synthesize? Also, when is declaring IBOutlet someObject enough? How is it that I can set/get UILabel value without @proper开发者_开发问答ty & @synthesize? Does it depend on UI object type?

And yes, I have read similar questions about these 2 :)


The pair (@property, @synthesize) will create the set/get methods used for accessing your ivars from other objects.

In a usual view controller you don't need to define properties for your IBOutlets since they should normally only be accessed by the view controller they belong to.


Highly recommended read: Using Properties in Objective-C Tutorial


property is needed only when you need the access to the member variables through the objects of that particular class. If you want to change some label's text at run time, that too accessing the object of the View Controller, then only you will need to have property defined for it, else not.

Outlet is just to make connection between an object from xib and a member from the class. If you want to give access to that member though object write property for it, else not.

Have a look at this


Its not needed if you dont want the variables or objects to be accessed outside the class by other objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消