((Any) throws -> Bool) throws -> Optional<Any>') 不能符合 'BinaryInteger'

((Any) throws -> Bool) throws -> Optional<Any>') cannot conform to 'BinaryInteger'

我在尝试转换来自 Apple Watch 的 WC 会话 [String: Any] 的消息时遇到问题,因为我正在自定义一个从 Apple Watch 检索心率并将其发送到 iPhone 的示例项目。我已经为 watchconnectivity 苦苦挣扎了 3 天,希望能得到一些帮助。

我确实看到我正在从 Apple Watch 正确接收消息,并且我即将利用收到的消息在 iOS 应用端更新心率。但我认为“guard let”语句导致 didReceiveMessage 函数中途停止。

我不确定,但我认为如果我可以将消息 ["rate": 61] 以双格式“展开”为 61,我应该能够通过 guard let 函数并使该函数正常工作。

最重要的是,我看到另一个错误说“NO with WCErrorCodeDeliveryFailed”,但我不确定为什么要填充它,因为我已经从其他 SO 答案中实施了建议的解决方案。但是我认为这个错误是 redundant/unrelated 上面的错误信息。

希望得到帮助。

import Foundation
import WatchConnectivity

protocol WatchKitConnectionDelegate: AnyObject {
    func didFinishedActiveSession()
    func updateLatestHeartRate(_ LatestHeartRate: Double)
}

protocol WatchKitConnectionProtocol {
    func startSession()
    func sendMessage(message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)?, errorHandler: ((NSError) -> Void)?)
}

class WatchKitConnection: NSObject {
    static let shared = WatchKitConnection()
    weak var delegate: WatchKitConnectionDelegate?
    
    private override init() {
        super.init()
    }
    
    func sendHeartRate(rate: Int) {
        WCSession.default.sendMessage(["rate": rate], replyHandler: nil) { error in
            print("Failed to send message: \(error)")
        }

        print("from iOS watchkit connection")
    }

extension WatchKitConnection: WatchKitConnectionProtocol {
    func startSession() {
        session?.delegate = self
        session?.activate()
    }
    
    func sendMessage(message: [String : AnyObject],
                     replyHandler: (([String : AnyObject]) -> Void)? = nil,
                     errorHandler: ((NSError) -> Void)? = nil)
    
    {
        validReachableSession?.sendMessage(message, replyHandler: { (result) in
            print(result)
            print("success in phone sendmessage function")
        }, errorHandler: { (error) in
            print("error in phone sendmessage function")
            print(error)
        })
    }
    
}

extension WatchKitConnection: WCSessionDelegate {

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
        print("didReceiveMessage from watch")
        print(message)
        
        delegate?.updateLatestHeartRate(Double(message.values.first))
        
        guard let heartRate = message.values.first as? String? else {
            return
        }
        guard let heartRateDouble = Double(heartRate!) else {
            return
        }
        
        print("printing heartRate double from message\(heartRateDouble)")
        
        delegate?.updateLatestHeartRate(heartRateDouble)
        
        print("updateLatestHeartRate")
      }

WatchKitConnection Swift 文件位于 iOS App

的帮助下,我能够正确解包消息并删除不必要的 guard let 语句到 运行 我的委托函数。下面是更新后的代码。

  1. message.values.first? 更改为 Int,因为费率是整数格式。
  2. 在将 heartRate 转换为双精度格式时删除了 guard let 语句,因为它是非可选的。
extension WatchKitConnection: WCSessionDelegate {

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
        print("didReceiveMessage from watch")
        print(message)
        
//        delegate?.updateLatestHeartRate(Double(message.values.first))
        
        guard let heartRate = message.values.first as? Int else {
            return
        }
        
        let heartRateDouble = Double(heartRate)
        
        print("printing heartRate double from message\(heartRateDouble)")
        
        delegate?.updateLatestHeartRate(heartRateDouble)
        
        print("updateLatestHeartRate")
        
    }
}