我是代码新手,并且使用最新版本的Xcode进行编码,并连接到Firebase,但始终会遇到此错误 我只看网络,但是找不到任何合适的解决方案。 请有人帮忙。
“ StorageMetadata”类型的值没有成员“ downloadURL”
这是我的代码:
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import SwiftKeychainWrapper
class SignUpVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var userImagePicker: UIImageView!
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var signUpBtn: UIButton!
var userUid: String!
var emailField: String!
var passwordField: String!
var imagePicker: UIImagePickerController!
var imageSelected = false
var username: String!
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
}
override func viewDidAppear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: "uid"){
self.performSegue(withIdentifier: "toMessage", sender: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
userImagePicker.image = image
imageSelected = true
} else {
print("image wasn't selected")
}
imagePicker.dismiss(animated: true, completion: nil)
}
func setUser(imag: String){
let userData = ["username": username!, "userImg": imag]
KeychainWrapper.standard.set(userUid, forKey: "uid")
let location = Database.database().reference().child("users").child(userUid
)
location.setValue(userData)
dismiss(animated: true, completion: nil)
}
func uploadImg() {
if usernameField.text == nil{
signUpBtn.isEnabled = false
}else{
username = usernameField.text
signUpBtn.isEnabled = true
}
guard let imag = userImagePicker.image, imageSelected == true else{
print("image need to upload")
return
}
if let imgData = imag.jpegData(compressionQuality:0.2){
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
Storage.storage().reference().child(imgUid).putData(imgData, metadata: nil, completion: {(metadata , Error)in
if Error != nil{
print("did not upload img")
}else{
print("uploaded")
let downloadURL = metadata?.downloadURL()?.absoluteString //Value of type 'StorageMetadata' has no member 'downloadURL'
if let url = downloadURL {
self.setUser(imag: url)
}
}
}
)}
}
@IBAction func cancel(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
@IBAction func selectedImgPicker(_ sender: UIButton) {
present(imagePicker, animated: true, completion: nil)
}
@IBAction func createAccount(_ sender: UIButton) {
Auth.auth().createUser(withEmail: emailField, password: passwordField, completion: {(user,Error) in
if Error != nil{
print ("Can't Create user! ")
} else {
if let user = user{
self.userUid = user.user.uid
}
}
self.uploadImg()
})
}
}