1

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)

}
2
  • 1
    It may caused because the value from "arc4random_uniform(UInt32(remainingCount))" has different type with 'i', i.e. one is UInt32 type, the other is Int type. You may try to cast it to Int: Int(arc4random_uniform(UInt32(remainingCount))) Commented Feb 20, 2016 at 18:26
  • Yes this worked thank you! Commented Feb 20, 2016 at 21:49

3 Answers 3

1

Try This

Updated Working Code

func shuffle(array: NSArray) -> NSArray {

        let newArray : NSMutableArray = NSMutableArray(array: array)

        let count : NSInteger = newArray.count

        for var i = 0; i < count; ++i {

            let remainingCount = count - i

            //figre out error below

            let exchangeIndex = i + Int(arc4random_uniform(UInt32(remainingCount)))

            newArray.exchangeObjectAtIndex(i, withObjectAtIndex: exchangeIndex)
        }

        return NSArray(array: newArray)

    }

Casted the result of arc4random_uniform to Int

Sign up to request clarification or add additional context in comments.

Comments

1

Swift 4.2

Now, from Swift 4.2 the feature is built into Swift. You can shuffle an array either in-place or create a new shuffled array.

var albums = ["Red", "1989", "Reputation"]

// shuffle in place
albums.shuffle()

// get a shuffled array back
let shuffled = albums.shuffled()

Comments

0

how to shuffle an array

import Foundation // arc4random_uniforn

extension Array {
    func shuffle()->Array {
        var arr = self
        let c = UInt32(arr.count)
        for i in 0..<(c-1) {
            let j = arc4random_uniform(c)
            if i != j {
                swap(&arr[Int(i)], &arr[Int(j)])
            }        
        }
        return arr
    }
}

let arr0 = [1,2,3,4,5,6,7,8,9,0]
for i in 0..<5 {
    print(arr0.shuffle())
}
/*
[6, 4, 0, 1, 8, 9, 3, 5, 2, 7]
[5, 2, 3, 9, 8, 1, 7, 6, 4, 0]
[8, 7, 6, 1, 2, 4, 9, 5, 0, 3]
[2, 4, 7, 6, 5, 9, 8, 1, 0, 3]
[7, 5, 6, 1, 9, 4, 2, 8, 3, 0]
*/

randomly and uniformly ... Why your code doesn't compile, is explained in note made by @z_px

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.