2011-12-15 93 views
1

我有我的應用程序mallocs內存結構對象下面的代碼。在代碼中多次調用此方法,每次爲新的Vector3D對象分配內存。將指針存儲到數組中的結構? iPhone

我想要做的就是捕獲指向mableced ShapeClass對象(C結構體)的指針,這樣我可以在以後釋放我的dealloc方法中的每個對象。

任何人都可以告訴我如何將指針添加到數組?

謝謝。

vectorCount = [floatArray.array count]/3; 
     ShapeClass *vectorArray = malloc(sizeof(Vector3D) * vectorCount); 
     for (int i=0; i<vectorCount; i++) { 
      vectorArray[i].x = [[floatArray.array objectAtIndex:(i*3)] floatValue]; 
      vectorArray[i].y = [[floatArray.array objectAtIndex:(i*3)+1] floatValue]; 
      vectorArray[i].z = [[floatArray.array objectAtIndex:(i*3)+2] floatValue]; 
     } 
     return vectorArray; 
// I WANT TO INSERT SOMETHING HERE TO CAPTURE THE POINTER TO vectorArray - E.G ADD IT TO AN ARRAY ? 
    } 
} 
return NULL; 
} 

回答

1

使用[NSValue valueWithPointer:]將其存儲在容器類中。

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; 
SomeObject *object = [[SomeObject alloc] init]; 

//Store the pointer to object 
[dictionary setValue:[NSValue valueWithPointer:object] forKey:@"object"];