无法将'NSURL.Type'类型的值转换为预期的参数类型'NSURL?'

时间:2016-08-06 18:00:49

标签: ios swift firebase swift2 xcode7

我正在尝试使用swiftfirebase构建聊天应用并收到此错误:下面是代码:我仍然无法理解错误! 我也在使用JSQMessageViewController

func sendMedia(picture: UIImage?, video: NSURL?) {
    print(picture)
    print(FIRStorage.storage().reference())
    if let picture = picture {
        let filePath = "\(FIRAuth.auth()!.currentUser?.uid)/\(NSDate.timeIntervalSinceReferenceDate())"
        print(filePath)
        let data = UIImageJPEGRepresentation(picture, 0.1)
        let metadata = FIRStorageMetadata()
        metadata.contentType = "image/jpg"
        FIRStorage.storage().reference().child(filePath).putData(data!, metadata: metadata) { (metadata, error) in
            if error != nil {
                print(error?.localizedDescription)
                return
            }
            let fileUrl = metadata!.downloadURLs![0].absoluteString
            let newMessage = self.messageRef.childByAutoId()
            let messageData = ["fileUrl": fileUrl, "senderId": self.senderId, "senderName": self.senderDisplayName, "MediaType": "PHOTO"]
            newMessage.setValue(messageData)
        }
    }else if let video = video {
        let filePath = "\(FIRAuth.auth()!.currentUser?.uid)/\(NSDate.timeIntervalSinceReferenceDate())"
        print(filePath)
        let data = NSData(contentsOfURL: video)
        let metadata = FIRStorageMetadata()
        metadata.contentType = "video/mp4"
        FIRStorage.storage().reference().child(filePath).putData(data!, metadata: metadata) { (metadata, error) in
            if error != nil {
                print(error?.localizedDescription)
                return
            }
            let fileUrl = metadata!.downloadURLs![0].absoluteString
            let newMessage = self.messageRef.childByAutoId()
            let messageData = ["fileUrl": fileUrl, "senderId": self.senderId, "senderName": self.senderDisplayName, "MediaType": "VIDEO"]
            newMessage.setValue(messageData)
        }

    }
}

}

extension ChatViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print("Did finish picking")
        //get the image
        print(info)
        if let picture = info[UIImagePickerControllerOriginalImage] as? UIImage {
            let photo = JSQPhotoMediaItem(image: picture)
            messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: photo))
            sendMedia(picture, video: nil)
        }
        else if let video = info[UIImagePickerControllerMediaURL] as? NSURL {
            let videoItem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
            messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: videoItem))
            sendMedia(nil, video: NSURL)
        }
        self.dismissViewControllerAnimated(true, completion: nil)
        collectionView.reloadData()
    }
}  

如果错误是NSURl的类型,我无法弄清楚如何解决它。

1 个答案:

答案 0 :(得分:1)

在这一行:

sendMedia(nil, video: NSURL)

您正在传递NSURL作为第二个参数video。 (请记住video:只是一个参数标签。)

也许你需要把它写成:

sendMedia(nil, video: video)