简体   繁体   中英

How can i call this function in another view controller?

I have a model in which i set this function up to check to see if the current user has a Role. I keep throwing errors when i try to call this code in a different view controller function...Im not quite sure on the syntax, its my first time trying to call a function like this. Thanks.

class AdminCheck {

  func getAdmin(completionHandler : ((Admin : Bool) -> Void)) {

var roleQuery = PFRole.query()
roleQuery!.whereKey("name", equalTo: "admin")
roleQuery!.getFirstObjectInBackgroundWithBlock() { (roleObject: PFObject?, error) -> Void in

  var adminRole = roleObject as! PFRole

  adminRole.users.query()!.findObjectsInBackgroundWithBlock({ (users, error) -> Void in


    if let objects = users {

      if objects == PFUser.currentUser() {

        completionHandler(Admin: true)

      } else {

        completionHandler(Admin: false)

my call is looking like this..

if AdminCheck.getAdmin {(Admin) -> void in

couple errors I'm getting... Binary operator '==' cannot be applied to operands of type '()' and 'Bool'

Here is an example. It's a good idea to play around with this in an XCode sample project.

class AdminCheck {    
    func amIAdmin(handler: (Bool) -> Void) {
        handler(false);
    }
}

var ad = AdminCheck()

ad.amIAdmin { (result:Bool) -> Void in
    print(result)
}

Something will like this.

class AdminCheck {

     class func getAdmin(completionHandler : ((admin : Bool) -> Void)){
          // your handle
     }
}

And use it.

AdminCheck.getAdmin { (admin) -> Void in
     // your handle
}

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