What is the difference between using self.var
vs. just var
in an Objective-C class? Are there benefits or dangers to one or the other?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
self.var
calls the property forvar
. Behind the scenes, Objective-C automatically generates a getter for properties (or you can make one yourself, if so inclined), soself.var
uses that getter. Plainvar
accesses the instance variable directly (i.e., it doesn't go through the getter to get the value).Duplicate of
and probably about a dozen others that you could've found in about 15 seconds by using the "Search" box up in the top right corner of the site.
is conceptually identical to
So using dot notation, you are really sending messages to self.
is conceptually the same as
So not using dot notation to access an instance variable is the same as treating self as a pointer to a C struct and accessing the struct fields directly.
In almost all cases, it is preferable to use the property (either dot notation or message sending notation). This is because the property can be made to automatically do the necessary retain/copy/release to stop memory leaks. Also, you can use key value observing with a property. Also subclasses can override properties to provide their own implementation.
The two exceptions to using properties are when setting an ivar in init and when releasing it in dealloc. This is because you almost certainly want to avoid accidentally using a sub class override in those methods and you don't want to trigger any KVO notifications.