Is it possible to assign the size of an array using NSMutableArray?

I used to be a Java programmer who had to declare an array for the first time, for example:

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

I don't know if Objective-C, NSMutableArray can give me this ability or not. In fact, I want to create a 10 * 10 array in advance. I am trying to do this:

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

But there are errors indicating my incompatible type assignment.

+4
source share
8 answers
  • The capacity field is rarely useful. In any case, the array will expand on demand.

  • And the capacity field just tells the array how much memory you can use. The array length is still 0.

  • But you can increase the array from empty:

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

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

    You cannot index NSArray directly.

  • There is a memory leak in your code. Unlike Java, ObjC does not use GC unless you explicitly turned it on (and ObjC on iPhoneOS does not have GC). ObjC manages memory by manually counting links. Basically, you need to make sure that the number of links you don’t own does not change in the process. See http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html for details.

    In your case, [[NSMutableArray alloc] …]; creates an object with the number ref +1, then the assignment will take over the array, which means that you no longer own it, but the number of links is not 0, so this memory will not be properly freed. You need to use convenient methods such as [NSMutableArray array…] to create an object with ref 0.

  • NSArray can store ObjC objects only. int in C (ObjC) is primitive and cannot be stored in NSArray. You must insert it into NSNumber [NSNumber numberWithInt:0] . You can return an integer with -intValue .

  • In conclusion, your code needs to be changed 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; } 
  • But ObjC is a superset of C. You can just use a simple 10x10 C array.

     int arr[10][10]; 
+10
source

You need a 10x10 array - what?

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

But these are errors telling my incompatible type.

Because you cannot assign myArray like this. myArray is an object that represents the data structure of an array. This is not an array of C.

If you need a 10x10 array of primitive data type, you can declare it the same way as in C:

 int myArray[10][10]; 
+1
source

You want NSMutableArray + arrayWithCapacity:

Note that setting the initial capacity is just an optimization - Mutable arrays expand as needed.

EDIT: To make the case 10x10,

 myArray = [[NSMutableArray alloc] initWithCapacity:10]; for (int i=0; i<10; i++) { NSMutableArray *subArray = [NSMutableArray arrayWithCapacity:10]; [myArray addObject:subArray]; for (int j = 0; j<10; j++) { [subArray addObject:[NSNumber numberWithInt:0]]; } } 

Notes:

  • the array saves the objects added to it, so it does not need to save subArray
  • Only objects can be added to NSArray (and not primitive types of type "int"), so the need for NSNumber numberWithInt:
  • you use methods like objectAtIndex: and replaceObjectAtIndex:withObject: to get / set the value from NSArray, not the index syntax ([])

See Apple links to NSArray and NSMutableArray

0
source

initWithCapacity: this is what you want. It might look like

 NSMutableArrat *array = [[NSMutableArray alloc] initWithCapacity:10]; 
0
source

NSArray objects have a fixed size, which cannot be changed after they are initialized. NSMutableArray objects can resize. A 10 × 10 array is sometimes implemented as an NSArray containing 10 separate NSArray objects, each of which contains ten elements. This quickly becomes cumbersome, sometimes it’s easier to return to simple C for such a task:

 int tenByTen[10][10]; 

Or you can use this:

 typedef struct { int y[10]; } TenInts; typedef struct { TenInts x[10]; } TenByTen; 

Then you could do:

 - (void) doSomethingWithTenByTen:(const TenByTen) myMatrix { NSLog ("%d", myMatrix.x[1].y[5]); } 

And you can also return them from methods:

 - (TenByTen) mangleTenByTen:(const TenByTen) input { TenByTen result = input; result.x[1].y[4] = 10000; return result; } 
0
source

You cannot access Cocoa array objects with a musical bracket. Your second bit of code should be:

 NSMutableArray *myArray = [[NSmutableArray alloc] initWithCapacity:10]; for (int i = 0; i < 10; i++) { [myArray insertObject:[NSMutableArray arrayWithCapacity:10] atIndex:i]; // Note: not using myArray[i]! } 
0
source

There are two ways to do this.

Plain old c

If you want to store objects, you should use the id type instead of int .

 int myarray[10][10]; myarray[5][2] = 412; 

Objective-c

NSArray should not have spaces without objects, if you need them, you can use [NSNull null] , but if in this case the C array would be better, though.

 NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:10]; for (int i=0; i < 10; i++) { NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:10]; for (int j=0; j < 10; j++) { [innerArray addObject:[NSNull null]]; } [myArray addObject:innerArray]; [innerArray release]; } [[myArray objectAtIndex:5] replaceObjectAtIndex:2 withObject:[NSNumber numberWithInteger:123]]; 
0
source

You can use the following code to resize NSMutableArray after creating it:

 @interface NSMutableArray (Resizing) - (NSMutableArray *)resize:(NSInteger)newSize; @end @implementation NSMutableArray (Resizing) - (NSMutableArray *)resize:(NSInteger)newSize { int size = (newSize > [self count]) ? self.count : newSize; NSMutableArray *array = [NSMutableArray arrayWithCapacity:size]; for (int i = 0; i < size; i++){ [array addObject:[self objectAtIndex:i]]; } return array; } @end 
0
source

Source: https://habr.com/ru/post/1306083/


All Articles