Adding structs to arrays in cocoa

Jump to First Reply
Posted:
in macOS edited January 2014
I want to store some NSPoints in an array. When I try to use the NSMutableArray:addObject method, the compiler gives me a bad arguement error. Is there a way to add structs to a NSMutableArray, or how else can i acomplish this?

Comments

  • Reply 1 of 6
    You could create an object with a single field of type NSPoint and add that object to the array. I don't know if this is the most, elegant solution, however. You should try posting your question to a cocoa-dev mailing list.



    -Chris
     0Likes 0Dislikes 0Informatives
  • Reply 2 of 6
    I'll try MacNN...
     0Likes 0Dislikes 0Informatives
  • Reply 3 of 6
    Try using [NSValue valueWithPoint:thePoint] to create objects from NSPoint structures.



    Edit: spelling



    [ 01-27-2003: Message edited by: Josef K. ]</p>
     0Likes 0Dislikes 0Informatives
  • Reply 4 of 6
    Seeing your code might help, but I'd guess that you tried to add objects, and not pointers to objects. I think you need to pass pointers to addObject. Example:



    // this isn't really good code, but it gets to my point.

    NSPoint mypoint;

    NSMutableArray *myArray = [[NSMutableArray alloc] init];

    [myArray addObject:&mypoint];
     0Likes 0Dislikes 0Informatives
  • Reply 5 of 6
    NSPoints are not Objective C objects. Check out NSGeometry.h:



    typedef struct _NSPoint {

    float x;

    float y;

    } NSPoint;



    typedef NSPoint *NSPointPointer;

    typedef NSPoint *NSPointArray;
     0Likes 0Dislikes 0Informatives
  • Reply 6 of 6
    dfilerdfiler Posts: 3,420member
    [quote]Originally posted by Josef K.:

    <strong>NSPoints are not Objective C objects. Check out NSGeometry.h:



    typedef struct _NSPoint {

    float x;

    float y;

    } NSPoint;



    typedef NSPoint *NSPointPointer;

    typedef NSPoint *NSPointArray;</strong><hr></blockquote>Yes, this is the key. NSArrays can only contain objects. Thus, you'll need to make the point an object with a statement like [NSValue valueWithPoint:thePoint]. NSValues are objects while NSPoints are not. Weird aye?
     0Likes 0Dislikes 0Informatives
Sign In or Register to comment.