How to populate table rows, using a [String] array sent from iPhone by Watch Connectivity?

元气小坏坏 提交于 2019-12-08 11:22:23

问题


Basically, it's passing the array fine. It's just when trying to use the enumerated array as the tablerows, it says nil found.

PHONE

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)
        }
    }
}

WATCHKIT

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)
                }
            }
        }

My problem is I'm getting this console output + error whenever I try and load the 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

Does anyone have any idea why the unwrapped value is still nil? I was hoping that once I get it set and can see that there are values in the [String] array, it could enumerate it, but it seems like they are just invisible.


回答1:


You started the question with the statement, "Basically, It's passing the array fine" so it's not a Watch Connectivity issue.

You simply didn't specify the number of rows for choiceTable before iterating through your array.

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

Determining the problem from the console output:

  • Error - attempt to ask for row 0. Valid range is 0..0

    rowControllerAtIndex: returns (an Optional which is) nil when its index is out of bounds.

    The row controller object, or nil if there are no row controllers yet or index is out of bounds.

    You tried to access a row which didn't exist, leading to a bounds warning.

  • fatal error: unexpectedly found nil while unwrapping an Optional value

    row2.choiceLabel.setText(thName)
    

    row2 is nil.

You should be able to easily track down bugs like this in the debugger. If you examined row2 and saw that it was nil, you'd realize that the problem wasn't with the array itself, but that the table has no rows.



来源:https://stackoverflow.com/questions/35596888/how-to-populate-table-rows-using-a-string-array-sent-from-iphone-by-watch-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!