0

In c++ i would do the following,

Object* obj1;
Object* obj2;
Object** targetObj;

void SetTargetToObj1()
{
    targetObj = &obj1;
}
void SetTargetToObj2()
{
    targetObj = &obj2;
}
void ValueChanged()
{
    //So if SetTargetToObj2() was called before ValueChanged() we 
    // would be changing some data on obj2
    (*targetObj)->ChangeSomeData();

    //or, we obj2 is null we could assign a new object to it via targetObj
    (*targetObject) = new Object();
    //now obj2 is pointing to our new object
}

Im wondering if there is a way in obj-c to do this same thing with NSObjects?

3
  • 2
    Isn't Obj-C a superset of C, and your code works fine in C?
    – Kerrek SB
    Aug 31, 2012 at 20:56
  • For instance, if i use NSDate. NSDate** targetDate; produces an error "pointer to non-const type 'NSDate *' with no explicit ownership"
    – Kyle
    Aug 31, 2012 at 21:06
  • What are you trying to do? I've never seen a need to write code like this in Obj-C. There's probably a better way to accomplish what you're trying to do.
    – kubi
    Aug 31, 2012 at 22:30

2 Answers 2

8

Pointers to pointers are not so simple under ARC.

When you declare, say, an instance variable:

NSObject *someObject;

you are implicitly declaring:

NSObject * __strong someObject;

i.e. a strong pointer. Strong is just one of the ownership qualifiers, you can also have weak and autoreleasing qualifiers.

Now taking the example in your comment:

NSDate **targetDate;

you get the error "pointer to non-const type 'NSDate *' with no explicit ownership". This is because ARC needs to know the ownership qualification of the pointer your pointer is referring to (read it slowly! ;-)). i.e ARC is asking you to type the variable instead as:

NSData * 'some ownership qualifer' * targetDate;

which, once you've decoded C's type priority rules, is a "pointer to a 'some ownership qualifier' pointer to an NSDate".

The error message includes "non-const" as this is all about writing via your pointer to pointer - ARC still needs to know how to handle the store, which depends on whether the pointed at reference is strong, weak, etc.

In your simple case the following should do:

NSObject *obj1;
NSObject *obj2;
NSObject * __strong * targetObj;

and then when doing (*targetObj) = ... etc. ARC knows what to do for memory management - which in this case is to release the old value in the variable referenced by targetObj as well as assigning the new reference into that variable.

Essential reading is Automatic Reference Counting and Transitioning to ARC Release Notes - in particular look up NSError in the latter as it explains how the common pattern of declaring error parameters as NSError ** is handled under ARC.

0
1

The code you have right there is already fine. If Object is in fact an obj-c object then this exact code is what you want. The only quirk is potential memory management issues (e.g. does targetObj need to retain the thing it's pointing to?)

3
  • targetObj points to obj1 or obj2 in this case. so targetObj is never the owner of an object it meanly points to the owner of an object. (I hope that makes sense)
    – Kyle
    Aug 31, 2012 at 21:10
  • 5
    It is "fine" in that it will "work". It isn't fine in that the use of pass-by-reference is extremely rare in Objective-C. That is a very odd pattern to see and often indicative of a serious design issue.
    – bbum
    Aug 31, 2012 at 21:31
  • Ignore the ValueChanged function, its a bad example of how i would use targetObj. targetObj will be used to save on duplicate code, meaning i can have 100 objects where they get set to the target object in some manor (like a selector callback) and then targetObj is how I reference those objects
    – Kyle
    Aug 31, 2012 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.