Showing posts with label NSArray. Show all posts
Showing posts with label NSArray. Show all posts

Saturday, July 21, 2012

convert NSArray in float[]


        NSArray *c=[NSArray arrayWithObjects:@"1",@"8",@"4",@"2",nil];
        
        NSEnumerator *enumerator;
        float * cArray;
        id obj;
        int index=0;
        
        cArray = (float *) malloc(sizeof(float) * [c count]);
        enumerator = [c objectEnumerator];
        while(obj = [enumerator nextObject])
        {
            cArray[index] = [obj floatValue];
            index++;
        }
        



       


Thursday, March 22, 2012

How to sort an NSArray with integers elements ?

 Its very very simple.



NSMutableArray *heightArray=[[NSMutableArray alloc]init];
for(int i=0; i<10; i++)
{
     float value=(rand()%100.0)/10;
     [heightArray addObject:[NSNumber numberWithFloat:value]];
}
NSArray *sortedArray = [heightArray sortedArrayUsingSelector:@selector(compare:)];
//Lets print the sorted Array
NSLog(@"sortedArray=%@",sortedArray);



For sorting an array which contains  instance of custom class. You can check this old post.



Thursday, June 16, 2011

Copy items of NSMutableArray into another ?

Lets say you have arrayA and arrayB, arrayB could be NSArray or NSMutableArray.
But arrayA should be mutable.


  [arrayA addObjectsFromArray:arrayB];





Here you are copying items from  arrayB to arrayA. Make sure you have already alloc memory for arrayA as:

arrayA = [[NSMutableArray alloc]init];




Case 2: If you want to create a copy an array to NSArray, you can do so as following:

NSArray *db=[[NSArray alloc]initWithArray:availListArray];