【发布时间】:2014-11-15 06:25:43
【问题描述】:
我正在使用 Multipeer Connectivity 在 Swift 中制作一个程序,以便在设备之间发送信息。我正在发送一个带有整数的数组,我需要将其转换为 NSDictionary,但是当我试图将其恢复为整数数组时,我无法将其恢复。我知道这是一种将 NSString 转换为 Integer 的方法,请参阅here,但我找不到数组的方法。有谁知道我该怎么做?
我在对等体中发送信息的代码如下所示:
let numberArray:[Int] = [4,76,23,65,23,75,23,65,12]
let messageDict = ["newNumbersArray":numberArray]
let messageData = NSJSONSerialization.dataWithJSONObject(messageDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
var error:NSError?
appDelegate.mpcHandler.session.sendData(messageData, toPeers: appDelegate.mpcHandler.session.connectedPeers, withMode: MCSessionSendDataMode.Reliable, error: &error)
在另一个对等体中,我的代码如下所示:
func handleReceivedDataWithNotification(notification:NSNotification){
let userInfo = notification.userInfo! as Dictionary
let receivedData:NSData = userInfo["data"] as NSData
let message = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary
}
如何将它转换回带有 int 的数组?
【问题讨论】:
-
你到底想做什么?在这里分享你的一些代码。
-
在接收代码中,记录消息并在此处显示。
-
{ nyeTall1 = (51,39,47,53,40,68,25,13,48 ); }每个数字都换行
标签: arrays swift nsstring xcode6