Tips : Upgrading your code from Swift 3 to Swift 4?

Do this before you convert your code

Tip#1

If your viewcontroller conforms to any protocol then do declare it along with the class definition so that Xcode could convert protocol methods into respective swift version.

For example We can assign UITableView/UICollectionView Delegate and Datasource directly from Storyboard and normally do not declare it in class definition and write our protocol methods directly, so is the case with other delegates like UISearchBar, UITextView or UITextField.

Just make sure that your ViewController contains the declaration

For example

class MyViewController : UIViewController, UITableViewDelegate,UITableViewDataSource

Why should you do this?

Xcode may not convert the already implemented method definition to the latest one as It may consider it your local function. You need to inform Xcode that your ViewController conforms to a protocol XYZ and hence Xcode will update your method definition too or at-least give you respective warnings or errors

Consequences?

If you don’t have all of ur code covered in UnitTesting or you have not implemented UITesting to test your app thoroughly there is a fair chance that you will miss some part of your code while doing manual testing. This may result in crashes or inappropriate behaviour.

This will also Make Sure that the future releases of Swift when doing conversion will convert your delegate methods automatically.

Better Approach (If you are still not following)

Always create extension of your Classes for protocol implementations

For example

extension MyViewController : UICollectionViewDelegate, UICollectionViewDataSource{

func numberOfSections(in collectionView: UICollectionView) -> Int {

return 1

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return 10

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “reuseIdentifier”, for: indexPath)

return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

Tip#2

If your code is a mixture of Swift and Objective-C then Swift 4 requires you to explicitly declare your swift methods and properties which are supposed to be used in Objective-C code. This is done by a writing a keyword @objc just before your property or method. This concept is called @objc inference and is supposed to decrease your build size. You can read more about this here:

In order to easily figure out the lines of code where you need to add @objc flag, all you need to do is to go to your build settings and search “Swift 3 @objc Inference” make the value to “Default”. It will throw errors at all those places where you need to place @objc literal. Just add @objc before the method or property and you are done. Xcode may throw warnings if you do not do the above but you may already have lot of warnings particularly due to Third-party libraries which have not yet migrated to Swift4. So converting @objc Inference warnings to errors will be handy.

From your Build Settings

P.S Swift 4 is not such a bigger change as was Swift 3 from Swift 2, Hence you will have enough information in terms of warnings and errors about where to change the code if Xcode already has not changed it. Most of it will be about string operations like Substring etc.

Here is the Official Migration Guide :

https://swift.org/migration-guide-swift4/