无法调用sendData

时间:2015-08-11 15:49:19

标签: ios swift multipeer-connectivity

我在尝试发送歌曲的当前时间时收到以下错误但是我得到了这个错误,但它说第一个参数应该是NSObject类型我很确定

Cannot invoke sendData with an argument list of type '(NSTimeInterval!, toPeers: [AnyObject]!, withMode: MCSessionSendDataMode)' 

func hostPhoneTime(){
    //songTime is of type NSTimeInterval!
    appDelegate.mpcHandler.session.sendData(songTime_, toPeers: appDelegate.mpcHandler.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable)



}

1 个答案:

答案 0 :(得分:2)

Apple docs中的

表示它是NSData,请查看

修改

以下是如何将NSIntervalTime(双倍)等值转换为NSData的示例

var time: NSTimeInterval = 112
// it's easier to parse your data to a String first
var timeString = "\(time)" 
// then this is how you get a NSData
let timeData = timeString.dataUsingEncoding(NSUTF8StringEncoding)!

// Let's imagine here you send your NSData to another place like a socket or something that receives NSData

// Your transform it back to a string
var receivedTimeString = NSString(data: timeData, encoding: NSUTF8StringEncoding)!
// And finally to your desired type
var receivedTime: NSTimeInterval = NSTimeInterval(Double(receivedTimeString as String)!)