MattDice
MattDice

Reputation: 195

Storing and retrieving CGPoints inside NSMutableArray

I've looked through countless questions on here and elsewhere and cannot for the life of me figure out what I'm doing wrong.

I'm trying to store an array of CGPoints as NSValues inside of an NSMutableArray named points like so on the iPhone:

NSValue *point = [NSValue valueWithCGPoint:firstTouch];
NSLog(@"NSValue *point = %@", point);
[points addObject:point];

NSLOG OUTPUT
NSValue *point = NSPoint: {120, 221}

Everything is going smooth converting from the CGPoint to NSValue. But when I try to retrieve the point I get nothing.

NSValue *getPoint = [points objectAtIndex:0];
CGPoint thePoint = [getPoint CGPointValue];
NSLog(@"Point = %@", NSStringFromCGPoint(thePoint));

NSLOG OUTPUT
Point = {0, 0}

The points should be the same but I'm getting a null result.

For testing purposes this is happening in the touchesBegan method.

Does anyone have any idea where I'm going wrong? Thanks in advance.

Upvotes: 4

Views: 4280

Answers (3)

Muzammil
Muzammil

Reputation: 1539

First allocate your mutable array in memory:

points = [[NSMutableArray alloc] init];

Then insert.

Upvotes: -1

mohitdream4u
mohitdream4u

Reputation: 166

for adding CGPoint to NSMutableArray

[ArrObj addObject:NSStringFromCGPoint(PointObj)];

for getting CGPoint from NSMutableArray

CGPoint pointObj2 = CGPointFromString([ArrObj objectAtIndex:index]);

may this help you..

Upvotes: 1

MattDice
MattDice

Reputation: 195

I never allocated my array into memory and initialized it. My points weren't being stored into an array because there was no array that existed.

Upvotes: 2

Related Questions