I have a method to shuffle an array randomly in Objective-C which works fine, but am having some trouble converting it. Here is the method in Objective-C...
- (NSArray *)shuffle:(NSArray *)array {
NSMutableArray *newArray = [NSMutableArray arrayWithArray:array];
NSUInteger count = [newArray count];
for (NSUInteger i = 0; i < count; i++) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t)remainingCount);
[newArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
return [NSArray arrayWithArray:newArray];
}
Here is the Swift version of the method, right below my "figure out error" comment it throws an error saying "Ambiguous use of operator '+'". I'm just trying to cast "remaining count" as a UInt32 for the arc_4random method so I'm not sure what's up. Any ideas? Thank you!
func shuffle(array: NSArray) -> NSArray {
let newArray : NSMutableArray = NSMutableArray(array: array)
let count : NSInteger = newArray.count
for var i = 0; i < count; ++i {
var remainingCount = count - i
//figre out error below
var exchangeIndex = i + arc4random_uniform(UInt32(remainingCount))
newArray.exchangeObjectAtIndex(i, withObjectAtIndex: exchangeIndex)
}
return NSArray(array: newArray)
}