View Full Version : ObjC: Problem with 2d array, NSPoints as NSValues

Jun 4, 2009, 06:02 PM

Using Objective C, I have a 2d array of NSValues holding NSPoints.
At least, that's what I'm TRYING to do.

Creation of 2d array, in init method:
intersects = [[NSMutableArray arrayWithCapacity:19] retain];
for (int i = 0; i < 19; i++) {
NSMutableArray *row = [NSMutableArray arrayWithCapacity:19];
[intersects addObject:row];

I did try to use "intersects = [intersects initWithCapacity:19];" but that didn't work. It just didn't seem to register at all.

Then in the method that finds the required points:
I get the 'row' I need to work on: NSMutableArray *row = [intersects objectAtIndex:rNum];
//Add point to 'row' array from 'intersects' - as NSPoint --> NSValue
NSValue *pt = [NSValue valueWithPoint:NSMakePoint(w, h+y)];
[row addObject:pt];
/*Also tried [row addObject:[pt copy]; Didn't seem to work either.*/
NSLog(@"(%i, %i)", w, h+y);
NSLog(@"%@", pt);
^ The NSLogs print out the right thing. eg:
(24, 24)
NSPoint: {24, 24}

Then I have a method which goes through the array and prints all the values - or at least, is supposed to:
- (void) printPoints
NSEnumerator *a = [intersects objectEnumerator];
NSMutableArray *row;
NSEnumerator *b;
NSValue *v;
while (row = [a nextObject]) {
b = [row objectEnumerator];
while (v = [b nextObject]) {
if (v = nil) {
}else {
NSLog(@"%@", v);

Which gives me this:

So, where am I going wrong?

Please help me. This is the (fingers crossed) last stumbling block before I'm on the home stretch with this project.

Jun 4, 2009, 07:52 PM
if (v = nil) {

should be

if (v == nil) {

or, my preferred form

if(!v) {

Jun 4, 2009, 08:00 PM
Disregard, I got confused.