Chetz Rupear Chetz Rupear - 4 months ago 40
Swift Question

Cannot convert value of type 'NSURL.Type' to expected argument type 'NSURL?'

Am trying to build a chat app using

swift
with
firebase
and getting this error: Heres the code below: I still can't understand what is wrong!!
Am using
JSQMessageViewController
as well.

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()
}
}


If the error is with the type of NSURl, i can't figure out how to fix it.

Answer

In this line:

sendMedia(nil, video: NSURL)

you are passing NSURL as the second parameter video. (Remember video: is just a parameter label.)

Maybe you need to write it as:

sendMedia(nil, video: video)