- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
class Parent: NSObject, NSCoding {
var name : String?
var childs : [Child]?
override init() {}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(childs, forKey: "childs")
}
required init(coder aDecoder: NSCoder) {
name = aDecoder.decodeObject(forKey: "name") as? String
childs = aDecoder.decodeObject(forKey: "childs") as? [Child]
super.init()
}
}
class Child: NSObject, NSCoding {
var name: String?
override init() {}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
}
required init(coder aDecoder: NSCoder) {
name = aDecoder.decodeObject(forKey: "name") as? String
super.init()
}
}
// iOS side:
public func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void) {
var parent = Parent()
parent.name = "John"
var child1 = Child()
var child2 = Child()
parent.childs = [child1, child2]
NSKeyedArchiver.setClassName("Parent", for: Parent.self)
let data = NSKeyedArchiver.archivedData(withRootObject: parent)
replyHandler(["parent": data as AnyObject])
}
// watchOS side:
if WCSession.isSupported() {
session = WCSession.default()
session!.sendMessage(["getParent": "getParent"], replyHandler: { (response) -> Void in
if let rawParent = response as? [String:Data] {
NSKeyedUnarchiver.setClass(Parent.self, forClassName: "Parent")
if let parent = NSKeyedUnarchiver.unarchiveObject(with: data) as? Parent {
// do something
}
}
}, errorHandler: { (error) -> Void in
print(error)
})
}
在这种情况下,正如您所看到的,我使用了 NSKeyedUnarchiver.setClass 和 NSKeyedArchiver.setClassName 方法,它们正在工作,但我仍然遇到运行时崩溃:[NSKeyedUnarchiverdecodeObjectForKey:]:无法解码 key (NS.objects)的类(Child)对象
如果我删除 NSKeyedUnarchiver.setClass(Parent.self, forClassName: "Parent")
和 NSKeyedArchiver.setClassName("Parent", for: Parent.self)
rows 我得到同样的错误,但对于父类。所以我确信我必须对 Child 使用相同的方法调用,但我不知道在哪里。有人有什么想法吗?
最佳答案
好吧,我找到了解决方案,如果您具有相同的结构,则只需简单地设置类名即可
NSKeyedArchiver.setClassName("Parent", for: Parent.self)
NSKeyedArchiver.setClassName("Child", for: Child.self)
当您取消存档时也是如此:
NSKeyedUnarchiver.setClass(Parent.self, forClassName: "Parent")
NSKeyedUnarchiver.setClass(Child.self, forClassName: "Child")
关于swift - 使用 NSKeyedArchiver 将对象传递给 WatchKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40320472/
我正在尝试创建一个 iOS WatchOS5 复杂功能,并且看到了许多教程和苹果文档。他们谈论: WatchKit 应用 WatchKit 应用扩展 iOS WatchKit 应用程序和 WatchK
我正在 Storyboard 中设置一个菜单和菜单项,并且希望一个菜单项在两种状态之间切换,例如开/关、摄氏度/华氏度等 但是,从菜单项按住控制拖动到界面 Controller 并不能让我创建导出,而
我必须将数据从我的 watchkit 发送到另一个 watchkit。可能吗 ?如果是,那么实现它的方法是什么? 我知道如何通过 [WKInterfaceController openParentAp
我在编译 watchkit 应用程序时遇到此错误 嵌入式二进制验证实用程序错误 error: WatchKit Extension contains multiple WatchKit apps wh
我们有一个分层的 watch 应用程序。 根 Controller 是一个菜单项表。该项目列表由服务器控制。数据被检索并存储在核心数据中。第一次进入应用程序时会填充菜单。 但我希望这张表保持最新。我的
我的应用程序显示各种倒数计时器,用户将查看这些计时器以了解何时执行各种任务。当应用程序位于前台时,我希望此屏幕继续显示在 watch 上。目前,屏幕会自动关闭,如果我不甩手腕,应用程序甚至会暂停。 这
谁能帮我编写一份示例代码,以便在 Apple Watchkit 应用中添加语音到文本转换功能。 最佳答案 是的,这是可能的。这是文档: https://developer.apple.com/libr
关于这个问题有几篇文章,但没有一个建议的解决方案适用于我的情况。我正在尝试按照 Apple 提供的说明将 WatchKit 集成到一个简单的示例应用程序中(我现在已经尝试了几个)。编译时出现以下错误消
昨天我saw this post并让我意识到我有问题。 在商店中有 WatchOS1 应用程序后,我创建了一个 WatchOS2 应用程序。当我尝试运行该应用程序时 made me upgrade m
iOS可以使用layer.cornerRadius打个圆UIButton . 是否WKInterfaceButton有这个属性吗? 如果没有,我怎么才能圆WKInterfaceButton在 Watc
错误信息: This app contains multiple WatchKit 2.0 apps. Only a single WatchKit 2.0 app is allowed. Watch
好吧,这是非常基本的,但我已经一遍又一遍地阅读了文档,并想确保我做对了。用通俗易懂的语言来说,我的 watchkit 应用程序将因退出我的代码外部应用程序的某些用户交互而关闭,对吗?我不需要使用任何类
在具有 WatchKit 扩展和 WatchKit 应用程序 - 目标的 xcode 项目中。如果我重命名目标 - 从例如:“Sample WatchKit App”和“Sample WatchKit
如果我将界面 Storyboard 的自定义类放在 WatchKit 或 WatchKist 扩展中,会有什么不同。 (以 myInterfaceController 为例) 最佳答案 您的 Watc
我有一个支持 Watchkit 的应用程序。当我在 iPhone 上运行该应用程序时,它构建良好,但在 Xcode 中显示警告“WatchKit 应用程序具有无效的 stub 可执行文件”。我已正确使
我在 WKExtensionDelegate 和 WKInterfaceController 上都添加了日志。 这是我在玩启动和退出应用程序时得到的结果 观看操作系统模拟器。 (模拟器运行 watch
验证 WatchKit 应用的 Info.plist 中 WKWatchKitApp 的值是否设置为 YES。 最佳答案 经过数周又数周的调查,我终于发现了问题所在。 Iphone App的产品名称与
我今天尝试将我的应用程序加载到 iTunesConnect,但在重新构建期间出现错误。没有找到配置文件(当我只为我的 iPhone 应用程序运行它时,它工作得很好并且已经有一段时间了)。所以我认为这是
我正在尝试将我的 Apple Watch 应用上传到 iTunes,每次我上传我的二进制文件时,我都会收到来自苹果团队的以下邮件。我已经完成了我的以下 list 。 为 watchapp 和扩展程序创
好的。在经典 iOS 中做到这一点很容易,但 WatchKit 不提供任何 block /闭包,并且在带/不带动画之间没有选择。 我有一个根 Controller ,其中有一个选项列表。触摸其中一个选
我是一名优秀的程序员,十分优秀!