chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

Swift Extension and Objective-C category

2017-04-22

Category in Objective-C, Extension in Swift are the same concept. They can help you to organise your class code. With category and extension, you don't have to create a inherited class.

Extension in Swift

Step 1: Create a Swift file

http://www.arkilis.me/wp-content/uploads/2017/04/WX20170421-205000.png

Then in the Swift file:

Step 2: Use extension:


Compared with the Category in Objective-C

Step 1: Create a Objective-C file

http://www.arkilis.me/wp-content/uploads/2017/04/WX20170421-205232.png

Select Category in the file type, and choose the class you want to extend:

http://www.arkilis.me/wp-content/uploads/2017/04/WX20170421-205648.png

Then it will create two files:

UIColor+HexColor.h

UIColor+HexColor.m

To use the category in your project, i.e. Change the background color of current view controller.

arkilis

Comments: