Search code examples
iphoneobjective-ccocoaios4

How to copy last three pieces data in an NSMutableArray to another NSMutableArray?


In an NSMutableArray I have data like this,

    MutableArray1:(
           "22.298166 , 73.165809",
           "22.300598 , 73.167183",
           "22.298101 , 73.166188",
           "22.298128 , 73.166194"
           "22.298130 , 73.166194"
          )

I want to copy the last three pieces of data from MutableArray1 to MutableArray2.

Please suggest how I can do that?


Solution

  • You can use subarrayWithRange: of NSArray like this:

    NSRange range = NSMakeRange([mutableArray1 count] - 3, 3);
    NSMutableArray *mutableArray2 = [NSMutableArray arrayWithArray:
                                      [mutableArray1 subarrayWithRange:range]];