尝试构建登录时出现条件绑定错误的初始化程序

时间:2017-08-17 20:18:06

标签: ios swift login runtime-error social-networking

我正在尝试完成社交媒体应用的登录页面,我有一个错误,我似乎无法弄明白。

以下是完整代码:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase
import SwiftKeychainWrapper

class UserVC: UIViewController, UIImagePickerControllerDelegate, 
UINavigationControllerDelegate

{

@IBOutlet weak var userImagePicker: UIButton!
@IBOutlet weak var CompleteSignInBtn: UIButton!
@IBOutlet weak var usernameField: UITextField!
var userUid: String!
var emailField: String!
var passwordField: String!
var imagePicker : UIImagePickerController!
var imageSelected = false
var username: String!


override func viewDidLoad(){
    super.viewDidLoad()

imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
}

func keychain(){
    KeychainWrapper.standard.set(userUid, forKey: "uid")
}
func imagePickerController(_ picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        userImagePicker.setImage(image,for:.normal)
        imageSelected = true
    } else {
        print("Image was not selected")
    }
    imagePicker.dismiss(animated: true, completion: nil)
}

func setUpUser(img: String ){
    let userData = [

        "username": username!,
        "userImg": img
    ]
    keychain()
    let setLocation = Database.database().reference().child(userUid)
    setLocation.setValue(userData)
}


func uploadImg() {

    if usernameField.text == nil {
        print("Please choose a username")
        CompleteSignInBtn.isEnabled = false
    } else {
        username = usernameField.text
        CompleteSignInBtn.isEnabled = true
    }

        guard let img = userImagePicker.image, imageSelected == true else {

        print("image must be selected")

        return
    }


    if let imgData = UIImageJPEGRepresentation(img, 0.2){
        let imgUid = NSUUID().uuidString
        let metadata = StorageMetadata()
        metadata.contentType = "img/jpeg"

        Storage().reference().child(imgUid).put(imgData, metadata: metadata) { (metadata, error) in
            if error =! nil {
                print("Did not upload")
            } else{
                print("Uploaded")
                let downloadURl = metadata?.downloadURL()?.absoluteString
                if let url = downloadURl {
                    self.setUpUser(img: url)
                }
            }
        }

    }
  }



@IBAction func completeAccount(_ sender: Any){
    Auth.auth().createUser(withEmail: emailField, password: passwordField, completion: { (user, error) in
        if error != nil {
            print("Error\(error)")
        } else {
            if let user = user {
                self.userUid = user.uid
            }
        }
        self.uploadImg() 
    })
    dismiss(animated: true, completion: nil)

}


@IBAction func seletedImagePicker(_ sender: Any){
    present(imagePicker, animated: true, completion: nil)
}

@IBAction func cancel(_ sender: AnyObject){
    dismiss(animated: true, completion: nil)
    }
}

导致标题中提到的错误的代码是:

guard let img = userImagePicker.image, imageSelected == true else {

        print("image must be selected")

        return
    }

我不知道该怎么做。我读到这与userimagePicker.image可能不是可选的这一事实有关,但在我正在遵循它的指南中有效。

谢谢!

1 个答案:

答案 0 :(得分:0)

您已将userImagePicker声明为UIButtonUIButton没有名为image的属性。您应该使用currentImage

guard let img = userImagePicker.currentImage, imageSelected == true else {
   print("image must be selected")
   return
}