DEV Community

Khoa Pham
Khoa Pham

Posted on

2 1

How to easily configure properties in Swift

This post lists the many ways that I know to easily configure properties in Swift

Using a helper method

var label: UILabel!

override func viewDidLoad() {
  super.viewDidLoad()

  configureLabel()
}

func configureLabel() {
  label = UILabel()
  label.backgroundColor = UIColor.greenColor()
  view.addSubview(label)
}

Using anonymous function

lazy var label: UILabel = { [weak self] in
  let label = UILabel()
  label.backgroundColor = UIColor.greenColor()
  return label
}()

Ah, by the way, did you know that

  • You shouldn't call access label in ViewController deinit, because it is lazy and we have weak self
  • lazy increases your compile time

@noescape configure Block on init

I first saw it on https://github.com/AliSoftware/Dip/blob/develop/Sources/Dip.swift#L61

public init(@noescape configBlock: (DependencyContainer->()) = { _ in }) {
    configBlock(self)
}

configure Block as extension

This https://github.com/devxoul/Then makes it easier to configure your property as an extension to NSObject

extension Then where Self: AnyObject {

    public func then(@noescape block: Self -> Void) -> Self {
        block(self)
        return self
    }
}

so we have

lazy var label: UILabel = UILabel().then { [weak self] in
    $0.backgroundColor = UIColor.greenColor()
}

We have to declare label: UILabel to use [weak self]

init without extension

I try to avoid extension, after reading this http://nshipster.com/new-years-2016/

public func Init<Type>(value : Type, @noescape block: (object: Type) -> Void) -> Type
{
    block(object: value)
    return value
}

we can use it like

lazy var label: UILabel = Init(UILabel()) { [weak self] in
  $0.backgroundColor = UIColor.greenColor()
}

We have to declare label: UILabel to use [weak self]

anonymous function again

This https://gist.github.com/erica/4fa60524d9b71bfa9819 makes configuration easier

lazy var label: UILabel = { [weak self] in
  $0.backgroundColor = UIColor.greenColor()
  return $0
}(UILabel())

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

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

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

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

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay