Daniel's working notes

Using if case let to match specific enum case

When working with enum and need to get associated value from one case, instead of switching and handle all the cases, we can use if case let keyword.

For example if we have enum like this:

import Foundation

enum Message {
	case text(userId: String, date: Date)
	case draft(userId: String, date: Date)
	case join(userId: String, date: Date)
	case leave(userId: String, date: Date)
	case balloon(userId: String, date: Date)
}

then we want to log userId when user send balloon message, we can use if case let

if case let Message.balloon(userId, date) = textMessage {
	log(userId)
}

Linked Notes: