Changing navigation title programmatically

You change the title by changing the title of the view controller being displayed: viewController.title = “some title” Normally this is done in view did load on the view controller: override func viewDidLoad() { super.viewDidLoad() self.title = “some title” } However, this only works if you have your view controller embedded in a UINavigationController. I … Read more

How can I use String substring in Swift 4? ‘substring(to:)’ is deprecated: Please use String slicing subscript with a ‘partial range from’ operator

You should leave one side empty, hence the name “partial range”. let newStr = str[..<index] The same stands for partial range from operators, just leave the other side empty: let newStr = str[index…] Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String‘s initialization … Read more

How do I write dispatch_after GCD in Swift 3, 4, and 5?

The syntax is simply: // to run something in 0.1 seconds DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code here } Note, the above syntax of adding seconds as a Double seems to be a source of confusion (esp since we were accustomed to adding nsec). That “add seconds as Double” syntax works because deadline … Read more

The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

I got rid of this warning by changing the “Swift 3 @objc Inference” build setting of my targets to “Default”. From this article: Before Swift 4, the compiler made some Swift declarations automatically available to Objective-C. For example, if one subclassed from NSObject, the compiler created Objective-C entry points for all methods in such classes. … Read more