canonical way to randomize an NSArray in Objective

2019-01-02 23:01发布

Is there a canonical way to randomize an array in Objective C?

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-02 23:39

There is none built into the SDK if that's what you are asking.

You can use just about any randomization or shuffling algorithm you want however. Different algorithms have different tradeoffs in terms of randomness, efficiency, etc.

http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms

For algorithms that shuffle "in-place" start with a mutable array use

insertObject:atIndex:
removeObjectAtIndex:

For algorithms that reconstruct the array, feed it the original and build a new array.

查看更多
登录 后发表回答