user2191116
user2191116

Reputation: 5

How can I create array of numbers in Objective-C?

i trying to create my firsy iphone program and i realize that making an array or matrix of 2 dims is difficult for me... :-(

*how and where i declarer somthing like this (take from java) so all the function can see it:

int[] myArray = new int[6];

*how can i trnslete this function:

  public  int[] sortArray (int[] myArray){
  int tmp;
  for (int x = 0; x < myArray.length; x++) {
   for (int y = x+1; y < 6; y++) {
    if (myArray[y] < myArray[x]) {
     tmp = myArray[x];
     myArray[x] = myArray[y];
     myArray[y] = tmp;
    }
   }
  }
  return myArray;
 }

*and how i call this function?

sortArray(myArray);

thanks for everyone!!! sharon

Upvotes: 0

Views: 5047

Answers (4)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

As in C,

int twoDArray[3][3];

In objective-C

NSArray *a=@[@"apple",@"axe",@"ant"];
NSArray *b=@[@"ball",@"book",@"baby"];
NSArray *c=@[@"cup",@"cat",@"cow"];

NSArray *twoDArray=@[a,b,c];

or in one statement:

NSArray *twoDArray=@[@[@"apple",@"axe",@"ant"],
                     @[@"ball",@"book",@"baby"],
                     @[@"cup",@"cat",@"cow"]];

EDIT:

NO need to convert that java function to obj-c method.

To sort the array :

NSArray *sortedArray = [array sortedArrayUsingComparator:^(id str1, id str2) {
    return [((NSString *)str1) compare:((NSString *)str2) options:NSNumericSearch];
}];

EDIT 2: (Removed unwanted typecast of nsstring to id and back to string)

NSArray *sortedArray = [array sortedArrayUsingComparator:^(NSString *str1, NSString *str2) {
    return [str1 compare:str2 options:NSNumericSearch];
}];

Upvotes: 3

foundry
foundry

Reputation: 31745

You seem to have (at least) two related-but separate questions here.

1/ how to create an array of numbers

Objective-C arrays come as immutable NSArrays (fixed contents) or mutable NSMutableArrays (you can add delete and shuffle contents around). You sort function as written is asking for a mutable array.

To create and populate an immutable array with NSNumber objects:

NSArray* array = @[@3,@5,@8,@2,@9,@1];   //"@1" is an NSNumber object literal
  //access: array[3] etc

Multidimensional:

NSArray* arrayOfArrays @[@[@3,@5,@8],@[@2,@9,@1]];
  //access: arrayOfArrays[1][2] etc

To create an empty variable-length mutable array

NSMutableArray* mutableArray = [[NSMutableArray alloc] init];

Create and populate a variable-length mutable array

myArray = [NSMutableArray arrayWithObjects:@3,@5,@8,@2,@9,@1, nil];  //note nil termination

To turn your immutable NSArray into a mutable NSMutableArray

NSMutableArray* mutableArray = [array mutableCopy];

(but take care, this will only render the top level as mutable, if it contains immutable subarrays they will remain immutable)

Objective-C collections (NSArray, NSDictionary, NSSet) can only hold objective-C objects. Therefore if you want to store ints or floats you need to box them into objective-C NSNumber objects before adding to a collection, and unbox them again to access the value.

int x;
float y;

NSNumber xNum = [NSNumber numberWithInt:x];  //box
NSNumber yNum = [NSNumber numberWithFloat:y];  //box


x = [xNum intValue]; //unbox
y = [yNum floatValue]; //unbox

2/ how to translate code

Here is a like-for-like translation:

To create the (mutable) myArray object:

NSMutableArray* myArray = [[NSMutableArray alloc] init];
Populate it:
[myArray addObjects:@3,@6,@8,@1,@9,nil];   //last value is nil to indicate termination

The method:

- sortArray:(NSMutableArray*)myArray
{
    id tmp;
    for (int x = 0; x < [myArray count]; x++) {
        for (int y = x+1; y < 6; y++) {
            if ([myArray[y] floatValue] < [myArray[x] floatValue]) {
                tmp = myArray[x];
                myArray[x] = myArray[y];
                myArray[y] = tmp;
            }
        }
    }
}

To call:

[self sortArray:myArray];

To declare with object scope, make a property in your @interface section

@interface myObject:NSObject
@property (nonatomic, strong) NSMutableArray* myArray;
@end

You will still need to create myArray before you can use it:

self.myArray = [[NSMutableArray alloc] init];

but you will be able to set and access it's values from anywhere inside the object thus:

self.myArray

And - if it is in the public header file @interface section - from outside the object thus:

myObject.myArray

Upvotes: 0

RyanCodes
RyanCodes

Reputation: 175

Declare in your respective .h file

NSMutableArray *numbers; 

Then in your .m file

numbers = [[NSMutableArray alloc] init];

 for (NSInteger i = 0; i < 6; i++)
     [numbers addObject:[NSNumber numberWithInteger:i]];

and declare it in your .h as

-(NSMutableArray *)sortArray:(NSMutableArray *)numbers;

This is the translated method above in Objective-C:

-(NSMutableArray *)sortArray:(NSMutableArray *)numbers
{
  NSInteger tmp = 0;
  for(int x = 0; x < [numbers count]; x++)
   for(int y = x + 1; y < 6; y++)
     if([numbers objectAtIndex:y] < [numbers objectAtIndex:x])
     {
       tmp = [numbers objectAtIndex:x];
       [numbers replaceObjectAtIndex:x withObject:[numbers objectAtIndex:y]];
       [numbers replaceObjectAtIndex:y withObject:tmp];
     }
  return numbers;
}

Also you can call a method in objective-c as follows:

[self sortArray:numbers];

Upvotes: 1

Adam
Adam

Reputation: 26917

You can do it with one line of code:

NSArray *array = @[@[@1, @2, @3],
                   @[@4, @5, @6],
                   @[@7, @8, @9]];

Learn about Objective-C literals here.

Upvotes: 5

Related Questions