xcode - iOS Swift 1.2 - Primitive Type Comparison Failure -
i have function gets , parses data firebase. have validation "parseusermodel) returns "bool" type if model meets requirements pass. set value validuser of bool type. model i'm pulling passes , returns boolean of true.
here's issue happens.
if(validuser) { ... code ... } fails though validuser "true".
i've played around actual return type , i've tried these things:
- validuser.boolvalue , setting inside if(validuser.boolvalue)
- this fails
- setting if(validuser) { ... code ... }
- this fails
- setting if(validuser == true) {..code..}
- this fails
- setting if(validuser.boolvalue == true) {code}
- this fails
i'm out of ideas why failing. i'm comparing primitive type (i'm assuming) primitive type.
my questions
- is 'true' keyword in swift not bool object type?
- why failing when i'm explicitly asking boolvalue?
- is swift bug should know about?
- is if statement not doing think should doing?
retrieve data function
public static func retrieveuserdata(user:usermodel, completion:(success:bool)->void){ if(user.username != nil){ let fbref = firebase(url: globalvariables().fb_base_url+"users/\(user.username)") fbref.observesingleeventoftype(feventtype.value, withblock: { (snapshot) -> void in if(snapshot.value != nil){ let validuser:bool = usermodel.sharedinstance.parseuserdata(json(snapshot.value)) println(validuser) println(validuser.boolvalue) if(validuser.boolvalue){ completion(success: true) } else { completion(success: false) } } }) } }
parse model , validate
public func parseuserdata(data:json) -> bool { var uname:string! var ufullname:string! if let username = data["username"].string { usermodel.sharedinstance.username = username } if let fullname = data["fullname"].string { usermodel.sharedinstance.fullname = fullname } else { return false } if let biography = data["biography"].string { usermodel.sharedinstance.biography = biography } if let email = data["email"].string { usermodel.sharedinstance.email = email } if let plistid = data["playlistcollectionid"].string { usermodel.sharedinstance.playlistcollectionid = plistid } else { //generate playlistcollectionid because user doesn't have 1 usermodel.sharedinstance.playlistcollectionid = nsuuid().uuidstring } if(usermodel.validateuserobject(usermodel.sharedinstance)) { return true } else { return false } }
println() log xcode true true
the above 2 true coming retrieval code println(validuser) , println(validuser.boolvalue)
the bool type in swift conforms booleantype, has member:
var boolvalue: bool { }
the value stored in validuser bool, because otherwise wouldn't able put in variable of bool type.
the situation here there bug in code. go on again , make sure it's entering else block instead of if block.
as side note, swift formatting convention if statements looks this:
if condition { ... } else { ... }
as opposed if(condition){...
Comments
Post a Comment