0

I have a class which must initialize a group of similar objects the same way.

NSNumber *a = nil;
NSNumber *b = nil;
NSNumber *c = nil;

a, b and c are member variables of an existing object. In my code I use a more complex NSObject subclass, but this example is easier to understand using NSNumbers.

All 3 objects must be initialized in a similar way. Therefore I want to build an array of pointers which I can use within a for-loop like this:

NSPointerArray *objs = [NSPointerArray weakObjectsPointerArray];
[objs addPointer:&a];
[objs addPointer:&b];
[objs addPointer:&c];

Running the above code I get the following error:

error: address doesn't contain a section that points to a section in a object file

How can I build an array which I can explore with a loop like this?

    for (id *o in objs) {
        *o = @2;
    }
3
  • 3
    Are you by any chance trying to store weak references in your array? Cause that's not supported by NSPointerArray. Commented Dec 22, 2012 at 17:58
  • what do you mean by not supported? The NSPointerArray has weak and strong pointer array constructors. I understand those constructors describe the week/strong relationship of the array pointers, not the pointers I insert. In my example, @[a,b,c] are strong pointers, and that is the reason I use the weakObjtsPointerArray constructor Commented Dec 22, 2012 at 19:49
  • Hmm. You're right, NSPointerArray can hold weak refereces. But the doc page for NSPointerArray says: "Important: NSPointerArray does not support weak references under Automatic Reference Counting (ARC)." I don't know how to interpret this, to be honest :) Commented Dec 22, 2012 at 22:42

1 Answer 1

5

just wrap the pointers in 'NSValue' objects and you're good

id v1 = [NSValue valueWithPointer:p1];
id v2 = [NSValue valueWithPointer:p2];
id v3 = [NSValue valueWithPointer:p3];

id array = @[v1,v2,v3];

for(NSValue *value in array) {
    void *pointer = value.pointerValue;
    NSNumber *n = (__bridge NSNumber*)pointer;
}

demo for what YOU want to do it appears: (NSNumbers are immutable)

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
@autoreleasepool {
    NSNumber *a;
    NSNumber *b;

    NSArray *os = @[ [NSValue valueWithPointer:&a],
                 [NSValue valueWithPointer:&b] ];

    for (NSValue *v in os) {
        void *pointer = [v pointerValue];
        NSNumber **n_ptr = (__bridge NSNumber**)pointer; 
        *n_ptr = @2.0;
    }

    NSLog(@"%@ %@", a,b);
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

it isnt as convenient as NSPointerArray but has fewer limits I think
OMG! so long ago since I had the mindset from C++ to wrap things in Value objects. That answers the first part of my question, but now I am having problems with the second part. I need to cast that (void )pointer into something I can use to change the value of the variable it references. Looking at my example I need something like (NSNumber**) but I cannot get it to compile. I am trying to fix the code at gist.github.com/4360450. I guess I am close to solve it myself, but I jumped to Cocoa after ARC and never had this fight with pointers :(
use id n = (__bridge NSNumber*) - added to sample
is that the right cast? I get an run-time error. Excuse me for using Gist and not StackOverflow, but over there I have version control of my edits and line numbers. I do what you say at line 9, and it seems wrong because I am assigning an (NSNumber**) to (NSNumber*). The following lines is what I assume it should look like, except that I cannot get Xcode to not freak out with the (NSNumber**) declaration: gist.github.com/4360450/…
just solved the ARC compatible version. The key was casting with (__bridge void*). Now finally got your answer ARC compatible
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.