如何使用如果让可选照片?

时间:2017-03-06 18:36:48

标签: swift xcode if-statement swift3

所以,我的应用程序崩溃,因为它强制解开“照片”,即使它是可选的,它内部没有任何内容。我如何使用if let语句,如果照片有图片然后它将显示,如果没有它将是nil但应用程序不会崩溃。 我有这个结构用户这是我保存数据的地方我正在使用firebase。

struct User {

    var fullName: String!
    var username: String?
    var email: String!
    var country: String?
    var photoURL: String?
    var biography: String?
    var uid: String!
    var ref: FIRDatabaseReference?
    var key: String?

    init(snapshot: FIRDataSnapshot) {

        key = snapshot.key
        ref = snapshot.ref

        fullName = (snapshot.value! as! NSDictionary) ["fullName"]  as! String
        email = (snapshot.value! as! NSDictionary) ["email"]  as! String
        uid = (snapshot.value! as! NSDictionary) ["uid"] as! String
        country = (snapshot.value! as! NSDictionary) ["country"] as! String?
        biography = (snapshot.value! as! NSDictionary) ["biography"] as! String?
        photoURL = (snapshot.value! as! NSDictionary) ["photoURL"] as! String?
        username = (snapshot.value! as! NSDictionary) ["username"] as! String?
    }

}

这是应用程序崩溃的地方,因为“self.storageRef.reference(forURL:imageURL!)”它强制解包,即使它里面没有任何东西。

 func loadUserInfo() {

 @IBOutlet weak var userImageView: UIImageView!
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var fullName: UILabel!
    @IBOutlet weak var username: UILabel!
    @IBOutlet weak var country: UILabel!
    @IBOutlet weak var biography: UILabel!

          let userRef = dataBaseRef.child("users/\(FIRAuth.auth()!.currentUser!.uid)")
            userRef.observe(.value, with: { (snapshot) in

                let user = User(snapshot: snapshot)
                self.username.text = user.username
               self.country.text = user.country
                self.biography.text = user.biography
                self.fullName.text = user.fullName
                var imageURL =  user.photoURL

self.storageRef.reference(forURL:imageURL!).data(withMaxSize: 1 * 1024 * 1024, completion: { (imgData, error) in

                if error == nil {
                    DispatchQueue.main.async {
                        if let data = imgData {
                            self.userImageView?.image = UIImage(data: data)
                        }
                    }
                } else {
                    print(error!.localizedDescription)
                }
            })
        })
        { (error) in
            print(error.localizedDescription)
        }
    }

2 个答案:

答案 0 :(得分:0)

首先我认为您应该更改用户的init,您应该这样做:

let data = snapshot.value as! NSDictionary;
fullName = data["fullName"] as! String;

如果你不确定country是否存在,你可以这样做:

country = data["country"] as? String;

然后您可以使用let在使用use.photoURL时保存,就像:

if let photoURL = user.photoURL {
     //...retrive photo of the url from somewhere
}

最后,我想说,你可能会误解?!,或者与他们混淆。

?是你认为这个变量/ func可能是nil /无法调用,或者当你进行类型转换时,但是你不确定它是否必须成功。

!是您确定它存在或您将立即创建它。你也可以把它理解为打开可选值,只是因为你让它绝对存在。

当我们像你的User一样创建我们自己的模型时,你可以使列不可能为零,你可以这样做:

var fullName: String?       = nil;
fullName = SomeJsonTypeData["fullName"] ?? "default value";

然后当你使用它时,你不用担心它会nil

答案 1 :(得分:0)

如果只关注photoURL问题,我认为这可能有助于您:

if let imageURL = user.photoURL {
    self.storageRef.reference(forURL: imageURL).data(withMaxSize: 1 * 1024 * 1024, completion: { (imgData, error) in
        if error == nil {
            DispatchQueue.main.async {
                if let data = imgData {
                    self.userImageView?.image = UIImage(data: data)
                }
            }
        } else {
            print(error!.localizedDescription)
        }
    })
}