简体   繁体   中英

Objective-c: Reference to ivar persistent? Good idea?

I have a situation where I'm keeping references to ivars which need to be persistent. In one object, I have an array of pointers to ivars in another object, which are used over the entire lifetime of the program. In other words, I'm not just passing a reference to retrieve a value -- I'm keeping the pointers around.

Is this a valid? Is it possible that the ivars might move? Are there cases where objects instantiated objects are moved around at runtime unbeknownst to the program? Or, do objects stay exactly where they are created. If the later is the case, is there any reason not to use references the way I am?

I'm using ARC.

Note: This probably wasn't a good way to design this to begin with, but... it's all done and working 99%! (except for a nasty crash which reboots the entire phone... )

Objects and their instance variables don't move once created. However, you also need to keep a strong reference to the object that holds the ivar. Otherwise, the object might be deallocated, leaving you with a dangling pointer.

Note that it is generally a very bad idea to have pointers to another object's insntance variables.

While there's no technical problem with accessing the ivars from outside (as rob stated) there's still the architectural design to consider: The approach you've taken breaks encapsulation. Additionally it is very uncommon for Objective-C.

So regarding maintainability of your code I would recommend to refactor the code. In Objective-C there's no friend declaration as in C++, so it's unusual to access ivars from outside the declaring class.

Let's say an object of class A wants to access the ivars of an object of class B persistently (in your example).

What you normally do is create a property (with the strong annotation, like @property (strong) ClassB *myBVar ) in class A to reference an object of class B. If you want to set or read B's properties you use the dot notation or call the getter/setter methods:

myBVar.name = @"Jim";
NSLog(@"Name:%@",myBVar.name);

[myBVar setName:@"Jim"];
NSLog(@"Name:%@",[myBVar name]);

You never call a ivar directly as it's implementation might change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM