将if((loc = [player locateCardValue:8])> - 1)转换为Swift 3

Joh*_*nne 2 objective-c code-conversion swift

你会如何转换这个Objective-C

if ((loc = [player locateCardValue:8]) > -1) {
Run Code Online (Sandbox Code Playgroud)

到Swift 3?

[player locateCardValue]返回'8'找到卡片的位置的整数.返回-1意味着它没有找到卡'8'.

我可以用......

let loc = player.locateCard(withValue: 8)
if loc > -1 {
Run Code Online (Sandbox Code Playgroud)

但我有多个IF的嵌套,它会变得非常混乱.

Mar*_*ino 5

也许最好的方法不是"按原样"转换它,而是让它更像Swift.

在这种情况下,我想我会更改locateCard为返回Optional<Int>nil在找不到卡时返回.

func locateCard(withValue: Int) -> Card? {
    // return the position if found, nil otherwise
}
Run Code Online (Sandbox Code Playgroud)

然后,你可以写

if let card = player.locateCard(withValue: 8) {

}
Run Code Online (Sandbox Code Playgroud)