我想收到一些用户在Apple Watch上输入的文字。这就是我到目前为止所做的:
presentTextInputController(withSuggestions: ["Michael","David","John","Lisa","Mary","Susan","Matthew","James","Jessica","Jennifer","Amanda","Emily","Dylan","Ross","Rupert"], allowedInputMode: WKTextInputMode.plain) { (arr: [Any]?) in
playerNames.playerOne = String(describing: arr)
print(playerNames.playerOne)
}
这总是返回一个可选的Optional([Michael])
我希望它返回Michael
我已经四处查看过期权,但似乎无法找到一个人。
答案 0 :(得分:1)
您可以尝试以下代码:
let suggestions = ["Michael", "David", "John", "Lisa", "Mary", "Susan", "Matthew", "James", "Jessica", "Jennifer", "Amanda", "Emily", "Dylan", "Ross", "Rupert"]
presentTextInputController(withSuggestions: suggestions, allowedInputMode: WKTextInputMode.plain) { (arr: [Any]?) in
guard let arr = arr, let firstElement = arr.first as? String else { return }
playerNames.playerOne = firstElement
print(playerNames.playerOne)
}
以下是您可以参考的一些文件:Optional Binding,Guard Statement。