I am trying to make objects that have simple C arrays in them. One is a straightforward array of integers, and another is an array of a structure. The values are 'correct' when the objects (simpleObj) are made, but once the objects are returned the values in the arrays are all messed up.
I use a NSMutableArray object to hold the simpleObjs. In this case, I simple make three such simpleObj. Each simpleObj has two arrays: one a ten number array of integers (all "5") and the other an array of a structure. The structure only holds two NSPoints (which are also structures).
For the life of me, I cannot figure out why the values are correct when made, but fall apart when passed out to the calling object. Help please!
I use simpleObj for the object definition (header and methods all in one file) and I use "main" to call the object.
Any help is greatly appreciated....
James
(Note: I'm not sure the two files (simpleObj.h and main.m made it as attached files - I'll keep trying if it does work via the "Attach Files" option)
Attaching didn't work - I'll have to figure that out later. I will paste the two files in here now.
Main:
Here is the simpleObj file, with header and methods combined:
I use a NSMutableArray object to hold the simpleObjs. In this case, I simple make three such simpleObj. Each simpleObj has two arrays: one a ten number array of integers (all "5") and the other an array of a structure. The structure only holds two NSPoints (which are also structures).
For the life of me, I cannot figure out why the values are correct when made, but fall apart when passed out to the calling object. Help please!
I use simpleObj for the object definition (header and methods all in one file) and I use "main" to call the object.
Any help is greatly appreciated....
James
(Note: I'm not sure the two files (simpleObj.h and main.m made it as attached files - I'll keep trying if it does work via the "Attach Files" option)
Attaching didn't work - I'll have to figure that out later. I will paste the two files in here now.
Main:
Code:
//
// main.m
// Arrays2Objects
//
// Created by JCTJ on 2/24/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "SimpleObj.h"
int main(int argc, char *argv[])
{
int numOfObjects = 3,
howManyElements = 10,
i;
SimpleObj * starterObject;
NSMutableArray * myArrayOfObjects;
// Create the array of objects
myArrayOfObjects = [[NSMutableArray arrayWithCapacity:numOfObjects] retain];
// Add the simple Objects to the array of objects
for (i = 0; i < numOfObjects; i++)
{
starterObject = [[SimpleObj alloc] initWithNumber:howManyElements]; // Each must be unique due to later manipulations
[myArrayOfObjects addObject:starterObject];
[starterObject release];
}
// Print the results
printf("The Final Results are.... \n");
for (i = 0; i < numOfObjects; i++) [[myArrayOfObjects objectAtIndex:i] spillMyGuts:i];
[myArrayOfObjects release];
return 0;
}
Here is the simpleObj file, with header and methods combined:
Code:
//
// SimpleObj.h
// Arrays2Objects
//
// Created by JCTJ on 2/24/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <stdio.h>
struct theStruct
{
NSPoint entry;
NSPoint exit;
};
@interface SimpleObj : NSObject
{
int numOfElements;
int * simpleArray;
struct theStruct * structArray;
}
// Actions
-(void)spillMyGuts:(int)theObjectNumber;
// Setters
// Accessors
// Overrides
-(id)initWithNumber:(int)theNumber;
-(void)dealloc;
@end
@implementation SimpleObj
// Actions
-(void)spillMyGuts:(int)theObjectNumber
{
int i;
printf("SimpleObj %i: Array = {", theObjectNumber);
for (i = 0; i < numOfElements; i++) printf("%i, ", simpleArray[i]);
printf("}\n Structure Array = \n");
for (i = 0; i < numOfElements; i++)
{
printf("%i: (%f, %f) (%f, %f) \n", i, structArray[i].entry.x, structArray[i].entry.y, structArray[i].exit.x, structArray[i].exit.y);
}
printf("\n \n");
return;
}
// Setters
// Accessors
// Overrides
-(id)initWithNumber:(int)theNumber
{
self=[super init];
if (self != nil)
{
int i;
int tempArray[theNumber];
struct theStruct tempStructArray[theNumber];
numOfElements = theNumber;
// Create the temporary arrays
for (i = 0; i < theNumber; i++)
{
tempArray[i] = 5;
tempStructArray[i].entry.x = 2;
tempStructArray[i].entry.y = 4;
tempStructArray[i].exit.x = 7;
tempStructArray[i].exit.y = 14;
} // i = 0 to theNumber
// Assign the temporary arrays to the pointers
simpleArray = tempArray;
structArray = tempStructArray;
// Print using the spillMyGuts method (number "0" means it is not being referenced from the NSMutableArray)
[self spillMyGuts:0];
}
return self;
}
-(void)dealloc
{
[super dealloc];
}
@end;