计算 Parse Class 中的对象数并将其分配给变量

Counting number of objects in a Parse Class and assign it to a variable

我想检索 class 中对象的数量,然后在徽章上显示该数字。

问题是,如果我写 self.totalMessagesInMesagesCount = count,我会得到这个错误:无法将类型 'Int32?' 的值分配给类型 'Int'[=29 的值=] 将该变量分配给 applicationIconBadgeNumber

所以我有:

var totalMessagesInMesagesCount : Int!

然后:

//MARK: this requires permission for notifications
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
            UIUserNotificationType.Badge, categories: nil
            ))
        //MARK: this sets the number displayed in the red badge
        UIApplication.sharedApplication().applicationIconBadgeNumber = totalMessagesInMesagesCount

最后我想检索数字并将其放入变量中

func loadMessagesByObjectData() {

        var findTimeLineDataQuery = PFQuery(className: KeyWords.ParseClassForPublicMessages)

        findTimeLineDataQuery.countObjectsInBackgroundWithBlock {
            (count: Int32, error: NSError?) -> Void in
            if error == nil {
                print("there are \(count) messages")
self.totalMessagesInMesagesCount = //what? count is not working

            }
        }


    }

提前致谢

听起来变量totalMessagesInMesagesCount来自类型Int,而变量countInt32类型。如此简单地通过像这样转换来解决它:

    self.totalMessagesInMesagesCount = Int(count)