swift - Custom protocols -
im getting error "class declaration cannot close on value 'viewcontainer' defined in outer scope"
i created procotol called networkresponse have 2 methods on sucessresponse , onerrorresponse.
then have class called callback extends networkresponse , forced implement methods.
here function :
public func login (callback : callback, viewcontroller : uiviewcontroller) { let callbackinstance: networkresponse = { class callback : callback { override func onsucessresponse(response : nsdictionary){ viewcontroller.dismiss(animated: true, completion: nil) } override func onerrorresponse(message : string, code : int){ print("error") } } return callback() }() postpath(callback: callbackinstance as? callback) }
i want dismiss controller anonymous class.
any recomendation ?
no need define protocol , callback class. closure need.
import uikit public class testinnerclass: uiviewcontroller { public func login(successcallback: ((response: nsdictionary) -> void), errorcallback: ((message: string, code: int) -> void)) { let success = false let response = nsdictionary() // // make login request here, , change `success` value depends on response // let response = ... // // if making async request login, put following codes inside request callback closure. // if success { successcallback(response: response) } else { errorcallback(message: "error occurred", code: -1) } } override public func viewdidload() { super.viewdidload() login({ (response) in // called when success self.dismissviewcontrolleranimated(true, completion: nil) }, errorcallback: ({ // called when failed (message, code) in print(message) })) } }
i have written sample codes case in github repo, , example using alamofire make network request, reference.
ps: since still using xcode 7.3.1, may need changes above code adopt swift 3 syntax requirement.
Comments
Post a Comment