我正在使用WatchConnectivity
从 iPhone 向 Watch 发送字符串值数组,但是这样做时出现以下错误。
无法将“__NSCFArray”(0x591244)类型的值转换为“NSString”(0x9f7458)。
我在将字典中的字符串数组发送到手表然后保存数组以在WKInterfaceTable
.
有谁知道我哪里出错了以及如何在手表上显示数组?
苹果手机
在收到手表发送数据的第一条消息后,iPhonedidRecieveMessage
会执行以下操作。
有一个名为的数组objectsArray
,每个对象都有一个名为 的字符串属性title
。我为所有值创建了一个新数组,title
并使用字典中的数组发送到手表。
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
var watchArray = [""]
for object in self.objectsArray {
watchArray.append(object.title)
}
print("Received message from watch and sent array. \(watchArray)")
//send a reply
replyHandler( [ "Value" : [watchArray] ] )
}
手表
var objectTitlesArray = ["String"]
//Display Array in WKInterfaceTable
func loadTableData() {
table.setNumberOfRows(self.tasks.count, withRowType: "CellRow")
if self.tasks.count > 0 {
for (index, objectTitle) in self.objectTitlesArray.enumerate() {
let row = self.table.rowControllerAtIndex(index) as! CellRowController
row.tableCellLabel.setText(objectTitle)
}
}
}
//Saving the Array
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
let value = message["Value"] as! [String]
dispatch_async(dispatch_get_main_queue()) {
self.objectTitlesArray = value
print("Received Array and refresh table")
loadTableData()
}
//send a reply
replyHandler(["Value":"Yes"])
}
更新
提到的错误似乎与将标签文本设置为值时的刷新操作有关。然而,在注释掉这些行之后,数组似乎仍然没有显示在 WKInterfaceTable 中,并且没有任何打印语句输出到控制台。