Farhad Faramarzi Farhad Faramarzi - 1 year ago 85
iOS Question

where do i use extension?

i know that the extension is for to extend types for which you do not have access to the original source code but some developers use extension in a class that themselves make it .

sorry for my english :(

example :

class Category: Mappable {
var PostCategoryId: Int?
var Name: String?
private(set) var Slug: String?
private(set) var PostCategoryParentId: Int?
private(set) var PostCategoryParentPath: String?
var childCategories: [Category]?
var parentCategory: Category?

init() {



extension Category {

enum CategoryId: Int {

case Comedy = 4
case Action = 6
case Animation = 10

Lew Lew
Answer Source

One use I find can be useful for separating your own logic from methods which must be implemented when conforming to a protocol. Example:

class MyClass {
  // My custom logic

extension MyClass : SomeProtocol {
  // Implement protocol methods here

This is purely a code organisation trick but I find it helps to keep things more maintainable. I'm sure there are some other uses developers have come up with so I'm looking forward to some of the other answers here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download