Wait For Asynchronous Operation To Complete in Swift

you have to pass your async function the handler to call later on: func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) { loadShows(completionHandler) } func loadShows(completionHandler: ((UIBackgroundFetchResult) -> Void)!) { //…. //DO IT //…. completionHandler(UIBackgroundFetchResult.NewData) println(“Background Fetch Complete”) } OR (cleaner way IMHO) add an intermediate completionHandler func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) … Read more

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

The [weak self] in anotherFunctionWithTrailingClosure is not needed. You can empirically test this: class Experiment { func someFunctionWithTrailingClosure(closure: @escaping () -> Void) { print(“starting”, #function) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { closure() print(“finishing”, #function) } } func anotherFunctionWithTrailingClosure(closure: @escaping () -> Void) { print(“starting”, #function) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { closure() print(“finishing”, #function) } } func … Read more

How to add a button with click event on UITableViewCell in Swift?

Popular patterns for solving this problem are closures and delegates. If you want to use closures, you would do something like this: final class MyCell: UITableViewCell { var actionBlock: (() -> Void)? = nil then @IBAction func didTapButton(sender: UIButton) { actionBlock?() } then in your tableview delegate: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) – > … Read more

Use reserved keyword a enum case

From the Swift Language Guide (Naming Constants & Variables section) If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice. enum MyEnum { … Read more

SwiftUI tappable subtext

Update for iOS 15 and higher: There is a new Markdown formatting support for Text, such as: Text(“Some text [clickable subtext](some url) *italic ending* “) you may check WWDC session with a timecode for details The old answer for iOS 13 and 14: Unfortunately there is nothing that resembles NSAttributedString in SwiftUI. And you have … Read more