I know it's possible to define public instance variable with @public keyword. However, Objective-C syntax does not allow accessing other class' variable. What features should I expected from @public Ivar? Or how do I access other class' Ivars?
相关问题
- 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?
- 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 “
- Why is my library not able to expand on the CocoaP
You can access it like using -> (member by pointer) operator like you normally access members of a c-structure (note that you always have a pointer to an object in obj-c):
You are supposed to write accessor methods to do that. Object-oriented design implies that a class shouldn't care about internal structure of objects of another class.
That said, id is just a pointer, so you can do obj->ivar, provided you know what you're doing and there is no way to write a proper accessor method.
Objective-C, as a superset of C, definitely does allow the access of public instance variables from outside the class's implementation. Now, the reason you may have heard that it isn't allowed is that it is highly discouraged. In most cases, if you want to access an instance variable outside an implementation context, you should be using accessors and mutators (properties).
An Objective-C class really boils down to a plain-old C struct with an
isa
field (that's what makes it an object), where the public fields are accessible. Since when we are dealing with instances of classes, we are working a pointer to an object (special struct). Thus, we access public fields using->
.Here's an example:
Now, in some completely different context, we could have:
I hope that helped you!