Steve DeRienzo
Steve DeRienzo

Reputation: 237

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'

Upvotes: 0

Views: 221

Answers (2)

pxr
pxr

Reputation: 40

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)
}

Upvotes: 1

Long Pham
Long Pham

Reputation: 7582

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
}

Upvotes: 0

Related Questions