NSArray for boolean array, define size...

Hi!

I have a Class that I'm trying to create...

The class have a boolean array that I define the size when the class initialize...

I put an NSArray in the .h file and I try to define the size of the array on initialisation based on a formula with the parameters for the init method.

I'm so new to Objective-C, I try to do that the same way I did it in C#..:S

What should I do?

Where Can I find information about class definition and sample with int or bool array?

Thanks!

Fred

MacBook, Mac OS X (10.5.5)

Posted on Nov 17, 2008 5:17 AM

Reply
13 replies

Nov 17, 2008 9:44 AM in response to jaxjason

Hi!

Here is some code...
------------------------------------------------
#import <Foundation/NSObject.h>

@interface Building: NSObject {
int width;
int height;
bool *roomSeen;
}

-(Building *) initWithWidth:(int) w height:(int) h;

@end
------------------------------------------------

------------------------------------------------
#import "Fraction.h"
#import <stdio.h>

@implementation Building
-(Building *) initWithWidth:(int) w height:(int) h
{
width = w;
height = h;
roomSeen = bool[width*height];
}
@end

------------------------------------------------


Can I do it with a NSArray?

I notice initWithObjects: count: but how can I define an array of X objects if I don't already have the objects?

Thanks!

Fred

Nov 17, 2008 11:36 AM in response to jaxjason

Hi!

This kinda help me... My problem is that I need to creates an array of boolean value of a specific size depending on the argument passed on the init method.

This array will be used by my class to monitor room seen by the user, since the number of room depend on the width and height of the building this array needs to be declare with a specific size.

This is an example made in C#, Can somebody put a equivalent in Objective-C?

------------------------------------------------
public class Building
{
private int width;
private int height;
private bool roomSeen[];

public Building (int width, int height)
{
this.width = width;
this.height = height;
this.roomSeen = new bool[width*height];

}
}
------------------------------------------------

Thanks!

Fred

Nov 17, 2008 9:11 PM in response to ShrimpBoyTheQuick

Hi!

My code was full of error...

Look now I've even put comments...

------------------------------------
public class Building
{
//private variable int for the width (instance var)
private int width;
//private variable int for the height (instance var)
private int height;
//private variable array of bool for roomSeen (instance var)
private bool[] roomSeen;

//Class constructor
public Building (int width, int height)
{
//Send parameter width to private variable width
this.width = width;
//Send parameter height to private variable height
this.height = height;
//Create a new array of bool of the size of width*height
this.roomSeen = new bool[[width*height]];

}
}
----------------------------------

I know this class isn't usefull at all but I need to understand how to create my class in Objective-C...

Nov 18, 2008 7:16 PM in response to ShrimpBoyTheQuick

Hi all!

I think I've something here but maybe I'm wrong...

I've looked at NSMutubleArray...

you can initWithCapacity, what I did without errors...

But when I try to use insertObject: atIndex: or replaceObjectAtIndex: withObject:

I got an error in the bottom of my screen "GDB: Program received signal: "EXC BADACCESS"" which I suppose is because I try to change an object with an index outside of the bounds since the capacity doesn't limit the NSMutableArray capacity...

Now I thing about the lamest way I think to do that would be to:
-write a little function that get an integer size as argument and return a NSArray
-when the function is called I create a new NSMutableArray, I do a For with the specified size and add a new NSNumber set to 0 to return an NSArray made from the NSMutableArray...

Maybe I'm missing some way to do a thing...

How would you do it?

Fred

Nov 20, 2008 5:17 AM in response to chrisbange

True, NSArray/NSMutableArray contain objects. So you definitely could use a C style array to easily store booleans.

But if you truly wanted to use an NSArray or NSMutableArray then you could stick the booleans into an object such as an NSNumber or NSString before adding them to the array. An example of this is property list files (.plist) which work with YES/NO, 1/0 values frequently. But since the OP didn't post any of his NSArray code it's not clear whether he was trying to do this or not.

Steve

Nov 20, 2008 6:22 AM in response to Steve Herman1

Hi!

I think I understand something here..

In C, I cannot put an unsized array in the .H file and set the size in the init method in the .m file...

What I can do is....

Define a pointer and a size in the .h file...
Create an array of the good size in the .m file and put the memory address of the array in the pointer and use this array with the pointer and the size value...

What's my problem after this is how do I tell the object to not used the memory of this array after the method is finished?

-------------------------------------------

#import <Foundation/NSObject.h>

@interface Building: NSObject {
int width;
int height;
int *arrayPtr;
int arraySize;
}

-(Building *) initWithWidth:(int) w height:(int) h;

@end
-------------------------------------------
-------------------------------------------
#import "Fraction.h"
#import <stdio.h>

@implementation Building
-(Building *) initWithWidth:(int) w height:(int) h
{
width = w;
height = h;
arraySize = width * height;
int roomArray [[arraySize]];
arrayPtr = &roomArray;

}
@end
-------------------------------------------

Is that the way I should do it and how to keep the roomArray memory?

Thanks!

Fred

Nov 20, 2008 7:36 AM in response to ShrimpBoyTheQuick

ShrimpBoyTheQuick wrote:
Hi!

I think I understand something here..

In C, I cannot put an unsized array in the .H file and set the size in the init method in the .m file...

Kinda true, but you can dynamically allocate memory at runtime to hold an array of whatever size you want.

What I can do is....

Define a pointer and a size in the .h file...
Create an array of the good size in the .m file and put the memory address of the array in the pointer and use this array with the pointer and the size value...

I don't think the code you posted will work right. I believe the "roomArray" that you're creating inside "initWithWidth" is a local variable that will go away once you exit the "initWithWidth" method. So even though you've stored a pointer to it in "arrayPtr" ultimately you'll be pointing at garbage once that memory gets reclaimed and reused by the runtime system.

What's my problem after this is how do I tell the object to not used the memory of this array after the method is finished?

Take a look at this discussion of dynamically allocated arrays in C. Basically you'll use malloc() to allocate memory for your array and then later use free() when you no longer need the array. And since you're dealing with width and height you're needing a two dimensional array which becomes somewhat messier.

Steve

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

NSArray for boolean array, define size...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.