传递字典观看 [英] Passing Dictionary to Watch
问题描述
我正在尝试使用后台传输通过 Application Context
方法通过 Watch Connectivity 从 iPhone 传递数据 -> Watch.
I'm trying to pass data from iPhone -> Watch via Watch Connectivity using background transfer via Application Context
method.
iPhone TableViewController
private func configureWCSession() {
session?.delegate = self;
session?.activateSession()
print("Configured WC Session")
}
func getParsePassData () {
let gmtTime = NSDate()
// Query Parse
let query = PFQuery(className: "data")
query.whereKey("dateGame", greaterThanOrEqualTo: gmtTime)
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objectsFromParse = objects as? [PFObject]{
for MatchupObject in objectsFromParse
{
let matchupDict = ["matchupSaved" : MatchupObject]
do {
try self.session?.updateApplicationContext(matchupDict)
print("getParsePassData iPhone")
} catch {
print("error")
}
}
}
}
}
}
我在日志中收到两次 error
print
ed (我在 Parse 中有两个 matchup
,所以它可能知道有两个对象,这就是为什么它也抛出两个错误?):
I'm getting error
twice print
ed in the log (I have two matchup
s in Parse so maybe it knows there's two objects and thats why its throwing two errors too?):
Configured WC Session
error
error
所以我什至还没有达到可以在 Watch 应用程序中打印
它以查看 matchup
是否正确传递的程度.
So I haven't even gotten to the point where I can print
it in the Watch app to see if the matchup
s passed correctly.
观看InterfaceController
:
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
let matchupWatch = applicationContext["matchupSaved"] as? String
print("Matchups: %@", matchupWatch)
}
有什么想法吗?将发布您需要的任何额外代码.谢谢!
Any ideas? Will post any extra code that you need. Thanks!
编辑 1:
根据 EridB 的回答,我尝试将编码 加入 getParsePassData
Per EridB answer, I tried adding encoding into getParsePassData
func getParsePassData () {
let gmtTime = NSDate()
// Query Parse
let query = PFQuery(className: "data")
query.whereKey("dateGame", greaterThanOrEqualTo: gmtTime)
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objectsFromParse = objects as? [PFObject]{
for MatchupObject in objectsFromParse
{
let data = NSKeyedArchiver.archivedDataWithRootObject(MatchupObject)
let matchupDict = ["matchupSaved" : data]
do {
try self.session?.updateApplicationContext(matchupDict)
print("getParsePassData iPhone")
} catch {
print("error")
}
}
}
}
}
}
但是在日志中得到这个:
But get this in the log:
-[PFObject encodeWithCoder:]:无法识别的选择器发送到实例 0x7fbe80d43f30
*** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
编辑 2:
根据 EridB 的回答,我也尝试将该函数粘贴到我的代码中:
Per EridB answer, I also tried just pasting the function into my code:
func sendObjectToWatch(object: NSObject) {
//Archiving
let data = NSKeyedArchiver.archivedDataWithRootObject(MatchupObject)
//Putting it in the dictionary
let matchupDict = ["matchupSaved" : data]
//Send the matchupDict via WCSession
self.session?.updateApplicationContext(matchupDict)
}
但是在函数的第一行得到这个错误:
But get this error on the first line of the function:
使用未解析的标识符MatchupObject
"
我确定我一定不明白如何正确使用 EridB 的答案.
编辑 3:
NSCoder
方法:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
//super.init(coder: aDecoder)
configureWCSession()
// Configure the PFQueryTableView
self.parseClassName = "data"
self.textKey = "matchup"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
推荐答案
错误
您收到该错误,因为您放置了一个不符合 NSCoding 在您要传递的字典中.
Error
You are getting that error, because you are putting a NSObject
(MatchupObject
) which does not conform to NSCoding inside the dictionary that you are going to pass.
来自 Apple 文档
对于大多数类型的传输,您提供一个 NSDictionary 对象您要发送的数据.字典的键和值必须都是属性列表类型,因为数据必须被序列化并且无线发送.(如果您需要包含不属于属性的类型列出类型,将它们打包在 NSData 对象中或将它们写入文件在发送之前.)此外,您发送的字典应该是压缩并仅包含您真正需要的数据.保持你的小字典确保它们被快速传输并做两台设备都不会消耗太多电量.
For most types of transfers, you provide an NSDictionary object with the data you want to send. The keys and values of your dictionary must all be property list types, because the data must be serialized and sent wirelessly. (If you need to include types that are not property list types, package them in an NSData object or write them to a file before sending them.) In addition, the dictionaries you send should be compact and contain only the data you really need. Keeping your dictionaries small ensures that they are transmitted quickly and do not consume too much power on both devices.
详情
您需要将NSObject's
存档到NSData
,然后将其放入NSDictionary
.如果您存档的 NSObject
不符合 NSCoding,NSData
将为 nil
.
Details
You need to archive your NSObject's
to NSData
and then put it in the NSDictionary
. If you archive a NSObject
which does not conform to NSCoding, the NSData
will be nil
.
这个示例极大地展示了如何符合NSObject
到 NSCoding
,如果你实现了这些东西,那么你只需按照下面的代码:
This example greatly shows how to conform a NSObject
to NSCoding
, and if you implement these things then you just follow the code below:
//Send the dictionary to the watch
func sendObjectToWatch(object: NSObject) {
//Archiving
let data = NSKeyedArchiver.archivedDataWithRootObject(MatchupObject)
//Putting it in the dictionary
let matchupDict = ["matchupSaved" : data]
//Send the matchupDict via WCSession
self.session?.updateApplicationContext(matchupDict)
}
//When receiving object from the other side unarchive it and get the object back
func objectFromData(dictionary: NSDictionary) -> MatchupObject {
//Load the archived object from received dictionary
let data = dictionary["matchupSaved"]
//Deserialize data to MatchupObject
let matchUpObject = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! MatchupObject
return matchUpObject
}
由于您使用的是 Parse,因此可能无法修改对象(我有一段时间没有使用 Parse,所以肯定是 IDK),但是从他们的论坛我发现了这个问题:https://parse.com/questions/is-there-a-way-to-serialize-a-parse-object-to-a-plain-string-or-a-json-string 可以帮助您比上面看起来更容易解决这个问题:)
Since you are using Parse, modifying an object maybe cannot be done (I haven't used Parse in a while, so IDK for sure), but from their forum I found this question: https://parse.com/questions/is-there-a-way-to-serialize-a-parse-object-to-a-plain-string-or-a-json-string which can help you solve this problem easier than it looks above :)
这篇关于传递字典观看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!