如何使用Watch Connectivity从iPhone发送的[String]数组填充表格行? [英] How to populate table rows, using a [String] array sent from iPhone by Watch Connectivity?

查看:161
本文介绍了如何使用Watch Connectivity从iPhone发送的[String]数组填充表格行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,它可以很好地传递数组.它只是在尝试将枚举数组用作表行时显示nil found.

Basically, it's passing the array fine. It's just when trying to use the enumerated array as the tablerows, it says 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时,都会出现此控制台输出+错误:

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

有人知道为什么解包后的值仍然为零吗?我希望一旦设置好它,并且可以看到[String]数组中有值,它可以枚举它,但似乎它们只是不可见的.

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.

推荐答案

您以基本上,它可以很好地通过数组"这样的语句开始了问题,因此这不是Watch Connectivity问题.

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

在遍历数组之前,您只是没有指定choiceTable的行数.

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

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

从控制台输出确定问题:

  • 错误-尝试询问第0行.有效范围是0..0

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

rowControllerAtIndex: 当其索引超出范围时返回nil(一个可选值).

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

行控制器对象,如果没有行控制器或索引超出范围,则为nil.

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.

严重错误:解开可选值时意外发现nil

fatal error: unexpectedly found nil while unwrapping an Optional value

row2.choiceLabel.setText(thName)

row2为零.

您应该能够在调试器中轻松跟踪此类错误.如果您检查row2并发现它为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.

这篇关于如何使用Watch Connectivity从iPhone发送的[String]数组填充表格行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆