常量 'spacesLeft' 推断为“()”类型,这可能是意外的 Swift
Constant 'spacesLeft' inferred to have type '()', which may be unexpected Swift
我正在使用 AI 使用 Xcode 8 和 Swift 构建井字游戏。以下是我正在使用的导致错误的相关变量:
var allSpaces: Set<Int> = [1,2,3,4,5,6,7,8,9]
var playerOneMoves = Set<Int>()
var playerTwoMoves = Set<Int>()
var nextMove: Int? = nil
在定义 AI 游戏方式的函数中有这些变量:
var count = 0
let spacesLeft = allSpaces.subtract(PlayerOneMoves.union(playerTwoMoves))
后者导致编译器警告:
Constant 'spacesLeft" inferred to have type '()', which may be unexpected
下面有一个 if 语句:
if allSpaces.subtract(playerOneMoves.union(playerTwoMoves)).count > 0 {
nextMove = spacesLeft[spacesLeft.startIndex.advancedBy(Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}
条件给出如下错误:
Value of tuple type '()' has no member 'count'
语句报错如下:
Type '()' has no subscript members
我正在努力寻找解决方案。
subtract
就地修改 Set
而不是 return 值,您想使用 subtracting
第一次警告,subtract
returnsVoid
,所以使用subtracting
:
let spacesLeft = allSpaces.subtracting(playerOneMoves.union(playerTwoMoves))
对于第二个错误,advancedBy
已弃用,你可以这样改:
if spacesLeft.count > 0 {
nextMove = spacesLeft[spacesLeft.index(spacesLeft.startIndex, offsetBy: Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}
Set.subtract
是一个变异函数,所以它修改了 Set
并且它的 return 值是 Void
,这只是一个空的类型别名元组,()
,因此警告。
您应该调用 Set.substracting
,它是 subtract
和 return 的非变异版本 Set<Set.Element>
。
subtract(_:)
函数是一个变异函数,因此它会改变 Set
您用来调用该函数的函数。
来自Apple Docs:
subtract(_:)
Removes the elements of the given set from this set.
你得到错误的原因是因为这个函数 returns Void
在 Swift 中是一个空元组的类型别名(from Swift's source code)。因为 Void
没有下标,也没有 count
property/variable 你会得到这些错误。
也许你应该看一下 subtracting(_:)
函数,returns 一个不同的 Set
。
来自 Apple Docs:
subtracting(_:)
Returns a new set containing the elements of this set that do not occur in the given set.
我正在使用 AI 使用 Xcode 8 和 Swift 构建井字游戏。以下是我正在使用的导致错误的相关变量:
var allSpaces: Set<Int> = [1,2,3,4,5,6,7,8,9]
var playerOneMoves = Set<Int>()
var playerTwoMoves = Set<Int>()
var nextMove: Int? = nil
在定义 AI 游戏方式的函数中有这些变量:
var count = 0
let spacesLeft = allSpaces.subtract(PlayerOneMoves.union(playerTwoMoves))
后者导致编译器警告:
Constant 'spacesLeft" inferred to have type '()', which may be unexpected
下面有一个 if 语句:
if allSpaces.subtract(playerOneMoves.union(playerTwoMoves)).count > 0 {
nextMove = spacesLeft[spacesLeft.startIndex.advancedBy(Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}
条件给出如下错误:
Value of tuple type '()' has no member 'count'
语句报错如下:
Type '()' has no subscript members
我正在努力寻找解决方案。
subtract
就地修改 Set
而不是 return 值,您想使用 subtracting
第一次警告,subtract
returnsVoid
,所以使用subtracting
:
let spacesLeft = allSpaces.subtracting(playerOneMoves.union(playerTwoMoves))
对于第二个错误,advancedBy
已弃用,你可以这样改:
if spacesLeft.count > 0 {
nextMove = spacesLeft[spacesLeft.index(spacesLeft.startIndex, offsetBy: Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}
Set.subtract
是一个变异函数,所以它修改了 Set
并且它的 return 值是 Void
,这只是一个空的类型别名元组,()
,因此警告。
您应该调用 Set.substracting
,它是 subtract
和 return 的非变异版本 Set<Set.Element>
。
subtract(_:)
函数是一个变异函数,因此它会改变 Set
您用来调用该函数的函数。
来自Apple Docs:
subtract(_:)
Removes the elements of the given set from this set.
你得到错误的原因是因为这个函数 returns Void
在 Swift 中是一个空元组的类型别名(from Swift's source code)。因为 Void
没有下标,也没有 count
property/variable 你会得到这些错误。
也许你应该看一下 subtracting(_:)
函数,returns 一个不同的 Set
。
来自 Apple Docs:
subtracting(_:)
Returns a new set containing the elements of this set that do not occur in the given set.