0

I have the following function written in Objective C using blocks and I am trying to convert it to swift, but I am banging my head against the wall and can't get it sorted.

Here is the code in Objective C

typedef void (^ResponseBlock) (BOOL succeeded, NSArray* data);

- (void)findAllMediaFromDate:(NSDate*)createdAtDate block:(ResponseBlock)block
{
NSMutableArray *results = [NSMutableArray array];
PFQuery *query = [PFQuery queryWithClassName:PARSE_MODEL_ACTIVITIES];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (ActivityObject *object in objects) {
            if ([object.media.type isEqualToString: @"video"] || [object.media.type      isEqualToString: @"photo"]) {
                [results addObject:object];
            }
        }

        block(YES, results);
    }
    else {

    }
}];
}

Now here is what I have in SWIFT. It's a different function body, but the syntax I am trying is the same...

func userQuery (){   //This needs to return an array as ABOVE
    var results = [UserModel]()
    println("NM: userQuery")
    var query = UserModel.query()
    query.whereKey("objectId", equalTo:"7N0IWUffOZ")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
        if (objects != nil) {
            NSLog("yea")
            for object in objects{
                results.append(object as UserModel)
                //Need to return the array to the userQuery function
            }

        } else {
            NSLog("%@", error)
        }

    }

}

```

1
  • @md-ibrahim-hassan Did you make a post on Meta before doing such mass edits? I think you should. Bumping up so many old posts with a tag that is not really useful is not ok IMO, you should ask community before doing this. Thanks. Commented Dec 23, 2018 at 18:20

1 Answer 1

2

You can add the closure parameter like so:

func userQuery(completionHandler: (succeeded: Bool, array: [UserModel]?) -> ()) {
    // do your other stuff here

    if objects != nil {
        // build results
        completionHandler(succeeded: true, array: results)
    } else {
        // return failure
        completionHandler(succeeded: false, array: nil)
    }
}

Clearly, change your array parameter to be whatever you want (rename it, change the type, whatever), but the basic idea is to have an optional array return type.

And you can call it using the trailing closure syntax, like so:

userQuery() {
    success, results in

    if success {
        // use results here
    } else {
        // handle error here
    }
}

By the way, if you like the Objective-C typedef pattern, the equivalent in Swift is typealias:

typealias ResponseClosure = (succeeded: Bool, array: [UserModel]?) -> ()

func userQuery(completionHandler: ResponseClosure) {
    // the same as above
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.