gpt4 book ai didi

swift - 如何使用 swift 2.2 watch 连接发送多个字典/多个 'application updates'

转载 作者:行者123 更新时间:2023-11-28 12:42:59 25 4
gpt4 key购买 nike

如何使用 Watch Connectivity 将多个“应用程序更新”从我的手机发送到我的 watch (例如数组中的几个不同值)?

我的应用程序更新成功地通过 TableView 中选定单元格的 numberItem 值发送,但我还想通过选定单元格数组的 userid 值发送。

现在,它只能识别一个值,不会更新另一个值,但会显示“请重试”作为我的标签文本。

如何通过两个或多个应用程序更新发送其他附加值(例如用户 ID 和用户名)。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let numberItem = number[indexPath.row]
print("tableview select #:")
print(numberItem)
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["number" : numberItem])
} catch {
let alertController = UIAlertController(title: "Oops!", message: "Looks like your \(numberItem) got stuck on the way! Please send again!", preferredStyle: .Alert)
presentViewController(alertController, animated: true, completion: nil)
}
let uidItem = 15
//let uidDict = ["uidValue":uidItem]
print("the send UID is")
//print(uidItem)
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["uidValue" : uidItem])
} catch {
let alertController = UIAlertController(title: "Oops!", message: "Looks like your \(uidItem) got stuck on the way! Please send again!", preferredStyle: .Alert)
presentViewController(alertController, animated: true, completion: nil)
}
}

我的 datasource.swift 文件是:

    enum Item {
case Number(String)
case uidValue(String)
case Unknown
}

init(data: [String : AnyObject]) {
if let numberItem = data["number"] as? String {
item = Item.Number(numberItem)
print("enum item is")
print(numberItem)
} else if let uidItem = data["uidValue"] as? String {
item = Item.uidValue(uidItem)
print("enum item is")
print(uidItem)
} else {
item = Item.Unknown
}
}

我 watch 上的接口(interface) Controller (连接到我的数据标签)包括:

func dataSourceDidUpdate(dataSource: DataSource) {
switch dataSource.item {

// the first application update- commented out to try the 2nd update
//case .Number(let numberItem):
// titleLabel.setText(numberItem)
// print(numberItem)

// the second application update
case .uidValue(let uidItem):
uidLabel.setText(uidItem)
print(uidItem)
case .Unknown:
nidLabel.setText("please retry")
default:
print("default")
}
}

最佳答案

您不能单独发送这些项目,因为 updateApplicationContext 会用最新数据替换任何较早的应用程序上下文数据。 the documentation 中的两个不同位置简要提到了这一点:

This method overwrites the previous data dictionary, ...

This method replaces the previous dictionary that was set, ...

自然地,Apple 优化了整个过程以提高能源/内存效率。在这种情况下,如果在第二数据排队等待传输时较早的应用上下文数据仍在待传输队列中,则可以丢弃较早的数据以避免不必要地传输/存储它。您的 watch 甚至不会收到第一个数据。

由于您的 watch 只会收到两条数据中的一条,这就解释了为什么当您检查收到的字典中的一个键时会看到“请重试”,但它只包含另一个键的数据。

如何一次传输多个项目

将两个项目包含在同一字典中,并使用单次传输传输该字典。

let data = ["number" : numberItem, "uidValue" : uidItem]
try WatchSessionManager.sharedManager.updateApplicationContext(data)
...

在 watch 端,您可以同时更新 title 标签和 uid 标签,而不是有条件地只更新一个或另一个。

if let numberItem = data["number"] as? String {
titleLabel.setText(numberItem)
}
if let uidItem = data["uidValue"] as? String {
uidLabel.setText(uidItem)
}

关于swift - 如何使用 swift 2.2 watch 连接发送多个字典/多个 'application updates',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com