我试图从String句子中获取第一个单词作为变量。怎么做?我有本地通知,我需要使用它的消息第一个字作为变量,有可能吗?对于LocalNotifications我使用LocalNotificationHelper库以便于处理。也许有逻辑问题,但我不这么认为。更像是我在Swift语言中没有足够的选择。
编辑:我还将nameField.text!中的NSUserDefaults作为数组。我需要从数组中删除消息的第一个单词。现在我只从数组中删除第一个对象,但这不是我的解决方案,因为我需要删除namefield.text!当LocalNotification弹出并用户单击按钮时从该数组中。
我在这里创建通知消息:

LocalNotificationHelper.sharedInstance().scheduleNotificationWithKey("text", title: "see options(left)", message: nameField.text!+"some text", date: deadlinePicker.date, userInfo: userInfo)

现在我需要message:nameField.text!在应用程序启动时变为变量。
这就是我触发通知按钮操作的方式:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "someFunction:", name: IDENTIFIER, object: nil)

最佳答案

可以使用空格字符拆分句子,然后提取第一个元素。

let sentence = "What a wonderful world"
if let firstWord = sentence.characters.split(" ").first.map(String.init) {
    print(firstWord) // "What"
}

10-08 05:50