DEV Community

Khoa Pham
Khoa Pham

Posted on

3 1

Instance property vs parameter in Swift

Original post https://github.com/onmyway133/blog/issues/72

The other day I was refactor my code. I have

extension MainController: TabBarViewDelegate {

  func buttonDidPress index: Int) {
    let initialIndex = tabBarView.selectedIndex
    let wholeAppContentView = updateWholeAppContentView()
    view.addSubview(wholeAppContentView)
  }
}
Enter fullscreen mode Exit fullscreen mode

The delegate method does not look right, as it's hard to tell between required delegate method, or just instance method. Also it lacks a subject. I like this post API Design, you can read section Rule 19: Always say who’s talking

This is a simple rule, and an equally simple mistake to make. In your delegate methods, always pass the sender as a parameter. Always. Even for singletons. Even for things you cannot conceive would ever be used more than once simultaneously. No exceptions.

So I refactor the delegate, and conform to it.

extension MainController: TabBarViewDelegate {

  func tabBarView(_ view: TabBarView, buttonDidPress index: Int) {
    let initialIndex = tabBarView.selectedIndex
    let wholeAppContentView = updateWholeAppContentView()
    view.addSubview(wholeAppContentView) // This is the culprit ⚠️
  }
}
Enter fullscreen mode Exit fullscreen mode

Even with just 1 line change in MainController.swift, the whole UI breaks, as all the views were added to the tab bar. Strange 😡 .

It didn't take long until I remember that parameter takes precedence over instance property if they have same name. So in this case, the compiler, without warning, assume you're dealing with view from TabBarView ⚠️

That's why you often use self to disambiguate.

struct User: Codable, Equatable {
  let firstName: String
  let lastName: String

  init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
  }
}
Enter fullscreen mode Exit fullscreen mode

Back to our code. The workaround is to specify self to specify view of MainController

self.view.addSubview(wholeAppContentView)
Enter fullscreen mode Exit fullscreen mode

Well, you may say, who should add view again in case of tab bar changes 😬 This is a bad example, but the lesson is learned 😇

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay