简体   繁体   English

PFUser.currentUser vs字符串

[英]PFUser.currentUser vs String

I'm working on iOS swift with Parse. 我正在使用Parse开发iOS Swift。 I've a code snippet as following: 我有一个代码片段,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    var currUser = PFUser.currentUser()

    var prefQuery = PFQuery(className: "preferences")
    prefQuery.whereKey("username", equalTo: currUser)

... ...

The above codes gives me nothing although I've one record in 'preference' table for the current user. 上面的代码没有给我任何好处,尽管我在'preference'表中为当前用户记录了一条记录。 But it returns a row when I use as : ... prefQuery.whereKey("username", equalTo: "Kale") 但是当我用作以下内容时,它会返回一行:... prefQuery.whereKey("username", equalTo: "Kale")

I'm confused what's wrong with using PFUser.currentUser() method to compare with a string? 我很困惑使用PFUser.currentUser()方法与字符串进行比较有什么问题?

Thanks in advance, 提前致谢,

You need to use: 您需要使用:

PFUser.currentUser()?.username

but because it is a optional you will need to unwrap the best way to do it is 但是因为它是可选的,所以您需要解开做到这一点的最佳方法是

if let user = PFUser.currentUser()?.username{
    //Query user 
    println("Found user \(user)")
} else {
    //Error handle
    println("No user logged")
}

I reviewed your code and I think PFUser.currentUser() is not of type NSString, you're getting confused with it. 我检查了您的代码,我认为PFUser.currentUser()的类型不是NSString,您对此感到困惑。 It contains dictionary of user information. 它包含用户信息字典。 Instead, get the valueofkey of it. 相反,获取它的valueofkey。 I think the key should be username. 我认为密钥应该是用户名。 So, replace this : 因此,替换为:

prefQuery.whereKey("username", equalTo: currUser.username)

Hope it helps.. :) 希望能帮助到你.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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