Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将洗牌数组方法从目标C转换为Swift(iOS)_Ios_Objective C_Arrays_Swift2 - Fatal编程技术网

将洗牌数组方法从目标C转换为Swift(iOS)

将洗牌数组方法从目标C转换为Swift(iOS),ios,objective-c,arrays,swift2,Ios,Objective C,Arrays,Swift2,我有一种在Objective-C中随机洗牌数组的方法,它工作得很好,但在转换它时遇到了一些问题。这是客观条件下的方法 - (NSArray *)shuffle:(NSArray *)array { NSMutableArray *newArray = [NSMutableArray arrayWithArray:array]; NSUInteger count = [newArray count]; for (NSUInteger i = 0; i < count; i++) {

我有一种在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];
}
-(NSArray*)随机:(NSArray*)数组{
NSMUTABLEARRY*newArray=[NSMUTABLEARRY arrayWithArray:array];
NSU整数计数=[newArray count];
对于(整数i=0;i
这里是该方法的Swift版本,就在我的“figure out error”注释下面,它抛出一个错误,说“运算符“+”的用法不明确”。我只是尝试将“剩余计数”转换为arc_4random方法的UInt32,所以我不确定发生了什么。有什么想法吗?谢谢大家!

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)

}
func shuffle(数组:NSArray)->NSArray{
让newArray:NSMutableArray=NSMutableArray(数组:数组)
let count:NSInteger=newArray.count
对于变量i=0;i
试试这个

更新的工作代码

func shuffle(数组:NSArray)->NSArray{
让newArray:NSMutableArray=NSMutableArray(数组:数组)
let count:NSInteger=newArray.count
对于变量i=0;i

将arc4random_uniform的结果强制转换为Int

如何洗牌数组

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]
*/
<代码>导入基础//ARC4Read 扩展阵列{ func shuffle()->数组{ var arr=自我 设c=UInt32(arr.count) 对于0..Swift 4.2中的i 现在,从Swift 4.2开始,该功能已内置到Swift中。您可以在适当的位置移动阵列,也可以创建新的移动阵列

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

// shuffle in place
albums.shuffle()

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

这可能是因为“arc4random_uniform(UInt32(remainingCount))”中的值与“i”的类型不同,即一个是UInt32类型,另一个是Int类型。您可以尝试将其强制转换为Int:Int(arc4random_uniform(UInt32(remainingCount)))是的,这很有效,谢谢!
var albums = ["Red", "1989", "Reputation"]

// shuffle in place
albums.shuffle()

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