Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assign array size using NSMutableArray?

I used to be a Java Programmer, which the array need to declare the very first time, like this:

int[] anArray;              // declares an array of integers
anArray = new int[10];      // allocates memory for 10 integers

I don't know whether the Objective C , NSMutableArray also give me this ability or not. Actually, I want to make a 10*10 array. thz in advance. I try to do this:

myArray = [[NSMutableArray alloc] initWithCapacity:10]; 

for (int i=0; i<10; i++) {
    myArray[i] = [[NSMutableArray alloc] initWithCapacity:10]; 
}

But it occurs errors, telling my incompatible type assignment.

like image 289
Tattat Avatar asked Dec 22 '22 04:12

Tattat


2 Answers

  1. The capacity field is seldom useful. The array will be expanded on demand anyway.

  2. And the capacity field just tells the array how much memory you may use. The array's length is still 0.

  3. But you can grow the array from empty:

     for (int i = 0; i < 10; ++ i)
         [myArray addObject:…];
    
  4. To read and write to an element in an NSMutableArray, you need:

     id x = [array objectAtIndex:i];              // x = array[i];
     [array replaceObjectAtIndex:i withObject:y]; // array[i] = y;
    

    You cannot subscript an NSArray directly.

  5. Your code has memory leak. Unlike Java, ObjC doesn't use a GC unless you explicitly enable it (and ObjC on iPhoneOS doesn't have GC). ObjC manages memory by manual reference counting. Basically you need to ensure the ref count of stuff you don't own doesn't change in the process. See http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html for detail.

    In your case, [[NSMutableArray alloc] …]; creates an object of ref count +1, then the assignment will take over the array, that means you don't own it any more, but the ref count is not balanced to 0, so this memory will not be properly deallocated. You need to use convenient methods such as [NSMutableArray array…] to create an object with ref count 0.

  6. NSArray's can only store ObjC objects. int in C (ObjC) is a primitive, and cannot be stored into an NSArray. You have to box it into an NSNumber by [NSNumber numberWithInt:0]. You can get back the integer with -intValue.

  7. To conclude, your code needs to be modified as:

      -(NSMutableArray*)get10x10Array {
         NSMutableArray* arr = [NSMutableArray array];
         for (int i = 0; i < 10; ++ i) {
            NSMutableArray* subarr = [NSMutableArray array];
            for (int j = 0; j < 10; ++ j)
               [subarr addObject:[NSNumber numberWithInt:0]];
            [arr addObject:subarr];
         }
         return arr;
      }
    
  8. But ObjC is a superset of C. You can just use a plain 10x10 C array.

    int arr[10][10];
    
like image 173
kennytm Avatar answered Jan 05 '23 10:01

kennytm


You want a 10x10 array -- of what?

myArray = [[NSMutableArray alloc] initWithCapacity:10]; 

for (int i=0; i<10; i++) {
    myArray[i] = [[NSMutableArray alloc] initWithCapacity:10]; 
}

But it occurs errors, telling my incompatible type assignment.

Because you can't assign to myArray like that. myArray is an object that represents an array data structure. It is not a C array.

If you want a 10x10 array of a primitive data type, you can declare one like you would in C:

int myArray[10][10];
like image 37
Shaggy Frog Avatar answered Jan 05 '23 10:01

Shaggy Frog