how can i use double Array like C?

Discussion in 'Mac Programming' started by junmoney83, Mar 7, 2010.

  1. junmoney83 macrumors newbie

    Joined:
    Mar 3, 2010
    #1
    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...
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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?
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. Daphtdazz macrumors member

    Joined:
    Nov 10, 2007
    Location:
    Oxford
    #4
    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!
     

Share This Page