-1

iPhone から Watch に文字列値の配列を送信するために使用WatchConnectivityしていますが、そうすると次のエラーが発生します。

タイプ '__NSCFArray' (0x591244) の値を 'NSString' (0x9f7458) にキャストできませんでした。

ディクショナリ内の文字列の配列を時計に送信し、配列を保存してWKInterfaceTable.

どこが間違っているのか、時計に配列を表示する方法を知っている人はいますか?

iPhone

ウォッチからデータを送信するための最初のメッセージを受信した後、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 に表示されていないようで、印刷ステートメントはコンソールに出力されません。

4

2 に答える 2

0

エラーが発生する場所は次のとおりです。

let value = message["Value"] as! [String]

上記では、ディクショナリでValueプロパティを取得しており、明示的にキャストすると. 次のようになります。messageString

if let value = message["Value"] {

    dispatch_async(dispatch_get_main_queue()) {
        self.objectTitlesArray = value as! [String]
    }
}

余談ですが、文字列の配列を別の冗長な配列にラップしているようにも見えます。

replyHandler( [ "Value" : [watchArray] ] )

文字列の配列を送信するだけの場合は、次のようにすれば十分です。

replyHandler( [ "Value" : watchArray ] )

于 2016-03-16T13:30:54.663 に答える