8

I am writing an Objective-C program that deals with low level image memory. I am using ANSI-C structs for my data storage -- Full blown objects seem overkill seeing as the data I am storing is 100% data, with no methods to operate on that data. Specifically, I am writing a customizable posterization algorithm which relies on an array of colors -- This is where things get tricky. I am storing my colors as structs of three floats, and an integer flag (related to the posterization algorithm specifically). Everyhting is going well, except for one thing...

[actual question]

I can't figure out how to add pointers to an NSMutableArray! I know how to add an object, but adding a pointer to a struct seems to be more difficult -- I do not want NSMutableArray dereferencing my pointer and treating the struct as some sort of strange object. I want NSMutableArray to add the pointer its self to its collection. How do I go about doing this?

Thanks in advance,

G

4 Answers 4

16

There's a class designed precisely for this: NSPointerArray. See Apple's docs here. For arbitrary pointers, you'll probably want to use the NSPointerFunctionsOpaqueMemory and NSPointerFunctionsOpaquePersonality options.

If NSPointerArray is not available (for example, if you're developing for iOS), then you could wrap your pointers using -[NSValue valueWithPointer:], as long as the overhead of wrapping & unwrapping doesn't prove too slow.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh my! I never would have thought! I'll take a look, thankyou :)
Cocoa has a lot of collection classes that most people miss--I find NSMapTable particularly useful. See the full list here: developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…
Oh! Thankyou! I will have to delve into that some time -- One thing, I am having issues getting the compiler to recognize NSPointerArray as a type -- Do you happen to know what header file I need to be importing for it to be recognized?
It's part of Cocoa--are you perhaps on iOS, developing with Cocoa Touch? In that case, unfortunately, NSPointerArray is not available and you should consider Carl's solution unless the wrapping & unwrapping overhead proves too slow. Sorry! :)
Ooooh dear, that is definitely it. Minor detail! My apologies for not having said that in the first place. Thank you very much anyhow for the great amount of detail and information you provided. I only wish I could accept both you, AND carl Norum's answers... I'll accept you due to the sheer fact that your answer is more detailed, and you came back to help me with any issues I had :)
4

You can wrap your pointers in NSValue objects.

Comments

3

example:

int aa=99;
int *a=&aa;
char bb='a';
NSMutableArray *MS=[NSMutableArray new];
[MS addObject:[NSValue valueWithPointer:a]];
[MS addObject:[NSValue valueWithPointer:&bb]];
int *c=[[MS objectAtIndex:0] pointerValue];
char *d=[[MS objectAtIndex:1] pointerValue];

Comments

1

If a NSMutableArray is really what you want, you can use this approach:

NSMutableArray * NewNonRetainingArray() {
  CFAllocatorRef allocator = 0; /* << default */
  CFIndex capacity = 0; /* << no maximum */

  /*
    0 for the callbacks will result in elements which are not reference counted.
    you can write your own callbacks, if you like some extra diagnostics or
    special behavior. see the CFArrayCallBacks structure for a full description
    of all callbacks.
  */
  const CFArrayCallBacks* const callbacks = 0;
  return (NSMutableArray*)CFArrayCreateMutable(allocator, capacity, callbacks);
}

However, you will not want to stuff NSObjects in there - they will not be retained.

Using this approach, your CF/NS-Array's elements may be pointers or any primitive value whose size is equal to a pointer.

Comments

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.