swift - 如何使用 Watch Connectivity 从 iPhone 发送的 [String] 数组填充表格行?

标签 swift watchkit watchos-2 watchconnectivity

基本上,它可以很好地传递数组。只是在尝试将枚举数组用作表行时,它说找不到。

电话

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

watch 套装

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] 数组中有值,它可以枚举它,但看起来它们只是不可见。

最佳答案

问题的开头是“基本上,它可以很好地传递数组”,所以这不是 watch 连接问题。

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

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

从控制台输出确定问题:

  • 错误 - 尝试请求第 0 行。有效范围是 0..0

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

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

    您试图访问不存在的行,导致边界警告。

  • fatal error :在展开可选值时意外发现 nil

    row2.choiceLabel.setText(thName)
    

    row2 为零。

您应该能够在调试器中轻松追踪此类错误。如果您检查 row2 并发现它是 nil,您就会意识到问题不在于数组本身,而是该表没有行。

关于swift - 如何使用 Watch Connectivity 从 iPhone 发送的 [String] 数组填充表格行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35596888/

相关文章:

swift - UICollectionView 在 Header 中添加 Action (Swift)

ios - 在 Swift 中重构解决方案

ios - 如何保存关系数据(核心数据)

ios - Apple Watch,无法安装 XXX,错误 : Application Verification Failed

watchkit - 如何在 Apple Watch App 中以编程方式在界面 Controller 之间创建下一页 segue?

xcode - 无法访问 WatchKit 应用 Assets

swift - 使用默认值处理可能的 nil 赋值的更好方法?

swift - 如何判断当前运行的 Apple Watch 尺寸/尺寸是 38 毫米还是 42 毫米?

ios - 使用 CoreData 时如何与 Watch OS 2 共享数据以显示在 WKInterfaceTable 中

xcode - Swift WatchOS 等待用户输入