How do you write a completion handler in Swift 3?

In Swift 3 the function parameter labels in closures are gone. Remove all occurrences of success: and add @escaping func Logout(completionHandler:@escaping (Bool) -> ()) { backendless?.userService.logout( { user in print(“User logged out.”) completionHandler(true) }, error: { fault in print(“Server reported an error: \(fault)”) completionHandler(false) }) } And use it Logout() { success in print(success) }

Whats the Swift animate WithDuration syntax?

Swift 3/4 Syntax Here’s an update with the Swift 3 Syntax: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { self.username.center.x += self.view.bounds.width }, completion: nil) If you need to add a completion handler just add a closure like so: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { // animation stuff }, … Read more

Change size of UIBarButtonItem (image) in Swift 3

This is how I did it iOS 10 and below func setUpMenuButton(){ let menuBtn = UIButton(type: .custom) menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 20) menuBtn.setImage(UIImage(named:”menuIcon”), for: .normal) menuBtn.addTarget(self, action: #selector(vc.onMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside) let menuBarItem = UIBarButtonItem(customView: menuBtn) self.navigationItem.leftBarButtonItem = menuBarItem } iOS 11 – Navigation bar come up with Autolayout so frame … Read more

try, try! & try? what’s the difference, and when to use each?

Updated for Swift 5.1 Assume the following throwing function: enum ThrowableError: Error { case badError(howBad: Int) } func doSomething(everythingIsFine: Bool = false) throws -> String { if everythingIsFine { return “Everything is ok” } else { throw ThrowableError.badError(howBad: 4) } } try You have 2 options when you try calling a function that may throw. … Read more

How to post nested json by SwiftyJson and Alamofire?

try this func test() { var exampleParameters : [String : Any] = [“b” : “bv”] exampleParameters[“a”] = [“a1”: “v1″,”a2”: “v2″] debugPrint(exampleParameters.description) let devUrlPush = URL.init(string:”yourURL”) var request = URLRequest(url: devUrlPush!) request.httpMethod = “POST” request.setValue(“application/json”, forHTTPHeaderField: “Content-Type”) request.httpBody = try! JSONSerialization.data(withJSONObject: exampleParameters) Alamofire.request(request).responseJSON { (response) in if( response.result.isSuccess) { }else { } } let string = … Read more

Redirect NSLog to File in Swift not working

The absoluteString property of an URL produces an URL string, e.g. file:///path/to/file.txt which is not suitable as argument to freopen(). To get the file path as a string, use path instead: let logPath = dir.appendingPathComponent(file).path Better, use the dedicated method to pass an URLs path to a system call: let logFileURL = dir.appendingPathComponent(file) logFileURL.withUnsafeFileSystemRepresentation { … Read more

techhipbettruvabetnorabahisbahis forumu