0
class Test: UIViewController{

    func abc() {
        print("This is ABC")
    }
}

extension Test {
    func def(){
        print("This is DEF")
     }
}

My question here is that

  1. what is the difference between both the methods declared?
  2. Is method def a static method?
  3. extending class to use protocols effects memory management?
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Veeral Arora
  • 139
  • 1
  • 7
  • 1
    read this document: https://docs.swift.org/swift-book/LanguageGuide/Extensions.html – Moayad Al kouz Aug 28 '18 at 10:05
  • 2
    Its depends on your creativity to organize your code. Btw, defining a function inside extension does not make it static. – Nizzam Aug 28 '18 at 10:08
  • Thanks for the link @MoayadAlkouz. I did read it earlier. The main concern is that Android said that iOS must not use extensions because the methods declared in extension are static and memory does not release. All iOS projects uses extension at some point and i have personally never faced any performance related issue. Is there anything that can explain them better? – Veeral Arora Aug 28 '18 at 10:26
  • @Nizzam I am using extension as below: extension AddDealsVC: FloatingMenuPressed{ func buttonPressed(isImage: Bool, isCamera: Bool, isVideo: Bool) { switch true { case isImage: break default: break } } } – Veeral Arora Aug 28 '18 at 10:32
  • 1
    @VeeralArora A method it its only static if you add the static keyword in front of its declaration otherwise it will be a public instance method as default and it doesn't make a difference if you declare it inside an extension or not – Leo Dabus Aug 28 '18 at 10:41

1 Answers1

0

what is the difference between both the methods declared?

None, except two things

  1. one prints ABC and another DEF, they have different names
  2. you won't be able to override the method from the extension

Is method def a static method?

Nope, for this, you should say static

extending class to use protocols effects memory management?

Nope

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100