简体   繁体   中英

iOS How to fetch value “Boolean” from parse Swift 4

I build app, in which i need to check if user verified email. My code to fetch data:

  let query:PFQuery = PFQuery(className: "_User");
        query.whereKey("objectId", equalTo: PFUser.current()!.objectId!)
        query.findObjectsInBackground { (object, error) in
            if !(error != nil) {
                let user = PFUser.current()
                let emailVerified = user!["emailVerified"] as! Bool
                print("emailVerified \(emailVerified)")
            }
        }

And i have error:

在此处输入图片说明

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

UPDATE:

So in my table i have value "false" 在此处输入图片说明

When code is running in console i receive value "true"

在此处输入图片说明

You can implement below extension as well for generic terms,

extension Integer {
    var boolValue: Bool { return self != 0 }
}

then use it like this,

let success = user!["emailVerified"].boolValue

UPDATE

You can do this way also,

let success = (user!["emailVerified"] as NSNumber).boolValue

another option will be like this,

extension Bool {    
    init(_ number: Int) {
        self.init(number as NSNumber)
    }
}

and get it like this,

let success = Bool(user!["emailVerified"] as NSNumber)

Try it out and let me know in case of any queries.

Use:

if let isVerify = user!["emailVerified"] as? NSNumber {
    let emailVerified = isVerify.boolValue
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM