【发布时间】:2014-12-22 20:51:25
【问题描述】:
我试着只写一个简单的冒泡排序函数:
func bubbleSort(array: [Int]) -> [Int] {
while !isSorted(array) {
for index in 1..<array.count {
if array[index - 1] > array[index] {
let temp: Int = array[index]
array[index] = array[index - 1]
array[index - 1] = temp
}
}
}
return array
}
检查,当数组被排序时,它使用:
func isSorted(array: [Int]) -> Bool {
for index in 1..<array.count {
if array[index - 1] > array[index] {
return false
}
}
return true
}
我使用http://swiftstub.com/ 编译代码,但它给了我以下错误消息:
<stdin>:17:17: error: '@lvalue $T11' is not identical to 'Int'
array[index] = array[index - 1]
^
<stdin>:18:17: error: '@lvalue $T8' is not identical to 'Int'
array[index - 1] = temp
^
(如果您想在网站上查看:http://swiftstub.com/385904096/)
array[index] 和 array[index - 1] 怎么不能都属于Int 类型,它们怎么可能属于不同类型?
【问题讨论】:
标签: swift