0

基本的に、配列をうまく渡しています。列挙された配列をテーブル行として使用しようとすると、nil found と表示されます。

電話

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

    @IBOutlet weak var sendButton: UIButton!

    var watchSession: WCSession?
    var arrayCustom = ["thing1", "thing2"]

    override func viewDidLoad() {
        super.viewDidLoad()

        if(WCSession.isSupported()) {
            watchSession = WCSession.defaultSession()
            watchSession?.delegate = self
            watchSession?.activateSession()
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBAction func sendArray(sender: AnyObject) {
        sendToWatch()

    }


    private func sendToWatch() {
        do {
            let applicationDict = ["Array1": arrayCustom]
            try WCSession.defaultSession().updateApplicationContext(applicationDict)
        }

        catch {
            print(error)
        }
    }
}

ウォッチキット

private func loadThCust() {

        if (WCSession.isSupported()) {
            watchSession = WCSession.defaultSession()
            watchSession.delegate = self;
            watchSession.activateSession()]
        }

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {

        dispatch_async(dispatch_get_main_queue()) { () -> Void in

            if let retrievedArray1 = applicationContext["Array1"] as? [String] {
                self.custArray = retrievedArray1
                print(self.custArray)
            }
            for (index, thName) in self.custArray.enumerate() {
                let row2 = self.choiceTable.rowControllerAtIndex(index) as! ChoiceTableRowController
                row2.choiceLabel.setText(thName)
                }
            }
        }

私の問題は、TableView をロードしようとするたびに、このコンソール出力 + エラーが発生することです。

["thing1", "thing2"]
2016-02-24 03:21:25.912 WristaRoo WatchKit Extension[9401:243561] Error - attempt to ask for row 0. Valid range is 0..0
fatal error: unexpectedly found nil while unwrapping an Optional value

アンラップされた値がまだゼロである理由を誰かが知っていますか? 設定して[String]配列に値があることを確認できたら、それを列挙できることを望んでいましたが、それらは見えないようです。

4

1 に答える 1

2

「基本的に、配列をうまく渡しています」という文で質問を始めたので、ウォッチの接続性の問題ではありません。

choiceTable配列を反復する前に行数を指定しなかっただけです。

choiceTable.setNumberOfRows(custArray.count, withRowType: "ChoiceTableRowController")

コンソール出力から問題を特定する:

  • エラー - 行 0 を要求しようとしました。有効な範囲は 0..0 です

    rowControllerAtIndex:インデックスが範囲外の場合、(Optional である) nil を返します。

    行コントローラー オブジェクト、または行コントローラーがまだない場合、またはインデックスが範囲外の場合は nil。

    存在しない行にアクセスしようとしたため、境界警告が発生しました。

  • 致命的なエラー: オプション値のラップ解除中に予期せず nil が見つかりました

    row2.choiceLabel.setText(thName)
    

    row2はゼロです。

デバッガーでこのようなバグを簡単に追跡できるはずです。調べrow2て nil であることがわかった場合、問題は配列自体にあるのではなく、テーブルに行がないことに気付くでしょう。

于 2016-02-24T10:59:06.357 に答える