iphone - How to swap value index in NSArray -
i have nsarray
. have value inside array.
nsarray *testarray = [nsarray arraywithobjects:@"test 1", @"test 2", @"test 3", @"test 4", @"test 5", nil]; nslog(@"%@", testarray);
result bellow :
( "test 1", "test 2", "test 3", "test 4", "test 5" )
now want result :
( "test 3", "test 5", "test 1", "test 2", "test 4" )
is there way without re-initialize array? can swap value of array ?
your array must instance of nsmutablearray
otherwise write methods not allowed (nsarray
read only)
use following method :
- (void)replaceobjectatindex:(nsuinteger)index withobject:(id)anobject
you need temporary storage storing replaces object :
id tempobj = [testarray objectatindex:index]; [testarray replaceobjectatindex:index withobject:[testarray objectatindex:otherindex]]; [testarray replaceobjectatindex:otherindex withobject:tempobj];
Comments
Post a Comment