Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

junmoney83

macrumors newbie
Original poster
Mar 3, 2010
20
0
there is some code...

@interfacd xxxClass

NSMutableArray *array[10];

@property(retain) array[10] ->>

@end


@implementation xxxClass

@synthesize ?? ->>

i don't know how i can adjust that "??"

i want to use point array...
 
If you want to use an array like in C, just use it like in C... Remember that Objective C is a superset of C. All C features are there.

Code:
double myArray [10];

What you tried to do is create an object that contains ten mutable arrays. Is that what you wanted? What do you actually want to do?
 
I would pick a way and stick to it. You cannot put primitives in NS(Mutable)Arrays, only NSObjects. Either make an NA(Mutable)Array of NS(Mutable)Arrays, or just use a 2D C array.

-Lee
 
Make sure you are clear about what you are trying to do. Objective-C was designed mainly to enable developers to write good applications efficiently. It is an API: you use it primarily to create applications.

C, on the other hand, was designed to write operating systems, so is very low level and as a result also is very good for writing efficient maths code.

So, if you want to do some maths in objective-C, you may want to try creating a custom object that is a wrapper for a double array. I've got ten minutes, here's an example:

@interface DoubleArray : NSObject
{
double *array;​
NSUInteger length;​
}

-(DoubleArray *)initWithSize: (NSUInteger)size;
-(void)dealloc;

-(NSInteger)getValue: (double *)value_dst fromIndex: (NSUInteger)index;
-(NSInteger)setValue: (double)value toIndex: (NSUInteger)index;
@end

@implementation DoubleArray

-(DoubleArray *)initWithSize: (NSUInteger)size{
[super init];​
if (size == 0) {[super dealloc]; return nil;}​
array = malloc(sizeof(double) * size) ;​
length = size;​
return self;​
}
-(void)dealloc{
free(array) ;​
[super dealloc];​
}

-(NSInteger)getValue: (double *)value_dst fromIndex: (NSUInteger) index{
if ( index >= length) return 1;​
*value_dst = array[index];​
return 0;​
}
-(NSInteger)setValue: (double)value toIndex: (NSUInteger)index{
if ( index >= length) return 1;​
array[index] = value;​
return 0;​
}

@end

This, of course, is very basic and you would almost certainly want methods to get the size of the array and maybe change its size as well. This code at least shouldn't cause you to read from or write to unallocated memory!

Now you've got this you can use NSArrays to store several instances of arrays, even 10, if you feel like it!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.