Edan Edan - 4 months ago 12
Swift Question

In Swift, how does one manipulate an object inside a typealias?

I have a typealias I would like to manipulate one of the objects in it after receiving it as s one completion handler and before sending it to another. So

typealias GalleryResponse = (gallery: MLGallery?, error: NSError?) -> ()


and the function i’d like to intervene on the typealias:

func getGalleryForDiscover(onCompletion: galleryResponse) {

let endpointURL = kGalleryURL + kMetaDataFilter + kLimitURL20

/// Would like to do something here with the MLGallery object in the galleryResponse closure.

makeRequestToCurbsAt(endpointURL, completionHandler: onCompletion)
}


How do I get to that
MLGallery
object - manipulate it - and then send it on?

Answer

You provide makeRequestToCurbsAt with its own completion handler, you then manipulate the MLGallary instance passed to that handler, and then pass it on to the original handler. Like such:

func getGalleryForDiscover(onCompletion: galleryResponse) {
    let endpointURL = kGalleryURL + kMetaDataFilter + kLimitURL20

    makeRequestToCurbsAt(endpointURL, completionHandler: {
        (gallery: MLGallery?, error: NSError?) in
      // do something with gallery

      // invoke the original
      onCompletion (gallery: gallery, error: error)
    })
}

Note: Really, really, your typealias identifier should be capitalized as GalleryResponse.