'Reverse NSMutableArray
What is the best/correct way to reverse an NSMutableArray?
Solution 1:[1]
theMutableArray=[[[theMutableArray reverseObjectEnumerator] allObjects] mutableCopy];
Solution 2:[2]
Here is the swift version of Reverse NSMutableArray
mutablearray = NSMutableArray(array: YourMutablearray.reverseObjectEnumerator().allObjects).mutableCopy() as! NSMutableArray
In pure Swift, you can use reverse()
let array = ["Apples", "Peaches", "Plums"]
for item in array.reverse() {
print("Found \(item)")
}
Source: hacking with swift
------ OR ------
var a = [String]()
a.append("1")
a.append("2")
a.append("3")
print(Array(a.reverse())) // In swift 3.0, method is `reversed()`
Solution 3:[3]
For Swift 3
let reversedArray = NSArray()
reversedArray = NSMutableArray(array:TableDataArr.reverseObjectEnumerator().allObjects).mutableCopy() as! NSMutableArray
Solution 4:[4]
you can reverse nsmutablearray with the help of reverseObjectEnumerator. It will return you nsarray then you should create a nsmutablearray with the help of this nsarray
NSArray* reversedArray = [[TableDataArr reverseObjectEnumerator] allObjects];
TableDataArr = [NSMutableArray arrayWithArray:reversedArray];
Solution 5:[5]
Why all the complication? Use this:
Swift 3:
let tasksArray = NSArray()
var reversedTaskArray = tasksArray.reversed() as NSArray
Solution 6:[6]
NSArray *Farr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
NSMutableArray *Reversearr1 = [[NSMutableArray alloc]init];
NSInteger j=[Farr count];
for (int i=1; i<=j; i++) {
[Reversearr1 addObject:[Farr objectAtIndex:j-i] ];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prasad G |
| Solution 2 | |
| Solution 3 | BHAVIK |
| Solution 4 | McDowell |
| Solution 5 | Josh O'Connor |
| Solution 6 | shyla |
