Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Ios Swift 3-否';排序';候选者产生预期的上下文结果类型';NSMutableArray';_Ios_Arrays_Swift_Sorting_Nsmutablearray - Fatal编程技术网

Ios Swift 3-否';排序';候选者产生预期的上下文结果类型';NSMutableArray';

Ios Swift 3-否';排序';候选者产生预期的上下文结果类型';NSMutableArray';,ios,arrays,swift,sorting,nsmutablearray,Ios,Arrays,Swift,Sorting,Nsmutablearray,我试图在Swift 3.1.1中对可变数组进行排序,但每次都会出现相同的错误: 没有“排序”候选项生成预期的上下文结果类型“NSMutableArray”。 有没有办法按升序对可变数组(仅限Ints)进行排序 在我的代码中,选项中的元素被删除。移除(数组)正在添加移除的元素。在代码的末尾,我试图将删除的数组中的元素添加回options并对其进行排序 // set up tiles var options = NSMutableArray() var removed = NSMutableArra

我试图在Swift 3.1.1中对可变数组进行排序,但每次都会出现相同的错误: 没有“排序”候选项生成预期的上下文结果类型“NSMutableArray”。 有没有办法按升序对可变数组(仅限Ints)进行排序

在我的代码中,选项中的元素被删除。移除(数组)正在添加移除的元素。在代码的末尾,我试图将删除的数组中的元素添加回options并对其进行排序

// set up tiles
var options = NSMutableArray()
var removed = NSMutableArray()
for i in 1...49 {
    options.add(i as Int)
    print("options\(options.count)")
}
for i in 1...49 {
    print("i = \(i)")
    options.remove(i)

    let tilea: Int = options[Int(arc4random_uniform(UInt32(options.count)))] as! Int
    options.remove(tilea)
    let tileb: Int = options[Int(arc4random_uniform(UInt32(options.count)))] as! Int
    options.remove(tileb)
    removed.add([i, tilea, tileb])
    print(options.count)

    if options.count < 20 {
        options.add(removed)
         options = options.sort {
             $0 < $1
        }
    }
}
//设置分幅
var options=NSMutableArray()
var removed=NSMutableArray()
因为我在1…49{
选项。添加(i作为Int)
打印(“选项\(options.count)”)
}
因为我在1…49{
打印(“i=\(i)”)
选项。删除(i)
让tilea:Int=options[Int(arc4random\u uniform(UInt32(options.count))]as!Int
选项。删除(tilea)
设tileb:Int=options[Int(arc4random_uniform(UInt32(options.count))]as!Int
选项。删除(tileb)
删除。添加([i,tilea,tileb])
打印(选项.计数)
如果选项数小于20{
选项。添加(删除)
options=options.sort{
$0 < $1
}
}
}

如前所述,在Swift中,您应该真正使用用于此目的的(也称为,
[T]
),而不是
NSMutableArray
。例如:

var options = [Int]()
向其添加元素时,请使用
append
(顺便说一下,您也可以删除类型转换):

最后,在对数组进行排序时,请使用
sort
函数(它不返回值;数组是就地排序的):


您甚至不需要提供比较函数,因为整数符合
compariable
协议

如前所述,在Swift中,您应该真正使用(也称为,
[T]
)而不是
NSMutableArray
。例如:

var options = [Int]()
向其添加元素时,请使用
append
(顺便说一下,您也可以删除类型转换):

最后,在对数组进行排序时,请使用
sort
函数(它不返回值;数组是就地排序的):


您甚至不需要提供比较函数,因为整数符合
compariable
协议

NSMutableArray
,以及其他的Objective C类型,被隐式地桥接到它的Swift对应项。为了减少人们(通常是不必要的)对这些目标C类型的依赖,Swift 3中改变了这种隐式桥接,现在需要显式类型强制(例如,
nsArray as[Int]

NSMutableArray
,以及其他目标C类型,隐式桥接到Swift的对应对象。为了减少人们(通常是不必要的)对这些目标C类型的依赖,Swift 3中改变了这种隐式桥接,现在需要显式类型强制(例如
nsArray as[Int]

NSMutableArray
是Obj-C,而
sort
仅适用于Swift数组,我想,改变你的
NSMutableArray()
[Int]()
这个算法是以一种非常粗略的方式编写的。我建议你最好解释一下你想要实现的目标,这样我们就能找到更好的解决方案。
NSMutableArray
是Obj-C,而
sort
只适用于Swift数组。我想,把你的
NSMutableArray()
改成
[Int]()
这个算法写得很粗略。我建议你最好解释一下你想要实现什么,这样我们才能找到更好的解决方案。很好的洞察力,我不知道:)顺便说一句,我想你是指显式类型强制,对吗?@PauloMattos是的,我的错误:pNice insight,我不知道:)顺便说一句,我想你是指显式类型强制,对吗?@PauloMattos是的,我的错误:p
options.sort()