Swift3 展开可选-Apple Watch文本输入

Swift3 展开可选-Apple Watch文本输入,swift3,watchkit,optional,Swift3,Watchkit,Optional,我想接收一些用户在Apple Watch上输入的文本。这就是我到目前为止所做的: presentTextInputController(withSuggestions: ["Michael","David","John","Lisa","Mary","Susan","Matthew","James","Jessica","Jennifer","Amanda","Emily","Dylan","Ross","Rupert"], allowedInputMode: WKTextInputMode

我想接收一些用户在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

我已经找遍了optionals,但似乎找不到anwser。

您可以尝试以下代码:

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)
}

以下是一些您可以参考的文档:,。

您希望它返回
“Michael”
还是
[“Michael”]
?只返回“Michael”。第一个问题是,如何将arr作为字符串返回?它工作得非常好,但返回-[Michael]。我怎么能只返回Michael呢?我刚刚编辑了我的答案,但只有
arr
始终返回一个元素,它才能正常工作。我需要您提供更多有关
arr
的上下文。例如:
arr
=
[“Michael”]
,它可以正常工作。但是如果
arr
=
[“James”,“Michael”]
,那么它将返回
“James”
,因为它将始终使用第一个元素。我假设
arr
总是有一个元素。是的,不客气!如果你需要帮助,请留言,我会回来的。