类不符合NSObjectProtocol [重复](Class does not conform NSObjectProtocol [duplicate])

编程入门 行业动态 更新时间:2024-10-27 07:17:19
不符合NSObjectProtocol [重复](Class does not conform NSObjectProtocol [duplicate])

这个问题在这里已经有了答案:

类型CCC不符合协议'NSObjectProtocol'2的 答案

我得到一个错误,我的类不符合NSObjectProtocol,我不知道这是什么意思。 我已经实现了WCSessionDelegate的所有功能,所以这不是问题。 有人知道问题是什么吗? 谢谢!

import Foundation import WatchConnectivity class BatteryLevel: WCSessionDelegate { var session: WCSession? { didSet { if let session = session { session.delegate = self session.activate() } } } var batteryStatus = 0.0; func getBatteryLevel(){ if WCSession.isSupported() { // 2 session = WCSession.default() // 3 session!.sendMessage(["getBatteryLevel": ""], replyHandler: { (response) -> Void in if (response["batteryLevel"] as? String) != nil { self.batteryStatus = (response["batteryLevel"] as? Double)! * 100 } }, errorHandler: { (error) -> Void in // 6 print(error) }) }} func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { } }

This question already has an answer here:

Type CCC doesnt conform to protocol 'NSObjectProtocol' 2 answers

I get an error that my class doesn't conform the NSObjectProtocol, I don't know what this means. I have implemented all the function from the WCSessionDelegate so that is not the problem. Does somebody know what the issue is? Thanks!

import Foundation import WatchConnectivity class BatteryLevel: WCSessionDelegate { var session: WCSession? { didSet { if let session = session { session.delegate = self session.activate() } } } var batteryStatus = 0.0; func getBatteryLevel(){ if WCSession.isSupported() { // 2 session = WCSession.default() // 3 session!.sendMessage(["getBatteryLevel": ""], replyHandler: { (response) -> Void in if (response["batteryLevel"] as? String) != nil { self.batteryStatus = (response["batteryLevel"] as? Double)! * 100 } }, errorHandler: { (error) -> Void in // 6 print(error) }) }} func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { } }

最满意答案

看看为什么在swift中我们不能没有从NSObject类继承协议?

简而言之, WCSessionDelegate本身从NSObjectProtocol继承,因此您也需要在该协议中实现方法。 实现这些方法的最简单方法是子类NSObject :

class BatteryLevel: NSObject, WCSessionDelegate

请注意,您正在处理Obj-C API。

See Why in swift we cannot adopt a protocol without inheritance a class from NSObject?

In short, WCSessionDelegate itself inherits from NSObjectProtocol therefore you need to implement methods in that protocol, too. The easiest way to implement those methods is to subclass NSObject:

class BatteryLevel: NSObject, WCSessionDelegate

Note that you are dealing with Obj-C APIs here.

更多推荐

本文发布于:2023-07-30 01:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不符合   NSObjectProtocol   Class   conform   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!