-1

Searching for days.

I saw this code at SwiftBySundall website.

enum UnboxPath {
    case key(String)
    case keyPath(String)
}

struct UserSchema {
    static let name = key("name")
    static let age = key("age")
    static let posts = key("posts")
    
    private static let key = UnboxPath.key
}

The text supplied was "Just like you can refer to a Swift function as a closure, you can do the same thing with enum cases with associated values:"

How does this work? How can I use this?

HangarRash
  • 7,314
  • 5
  • 5
  • 32
iPadawan
  • 898
  • 1
  • 12
  • 23
  • Are you simply asking about enum associated values? If so, see [Associated Values](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/enumerations#Associated-Values) in the Swift book. – HangarRash Apr 05 '23 at 19:53
  • I think you have misread the text, it doesn't say you can refer to an enum case as a closure. The first part about function as a closure most likely refers to the fact that you can assign a function to a variable and pass it around (just like a closure). So I assume it's something similar for the enum case but I am not completely sure. – Joakim Danielson Apr 05 '23 at 20:31
  • @iPadawan Not my downvote but hover your mouse pointer over the downvote arrow and a tooltip will appear showing the major reasons someone might downvote a question. My guess is someone felt your question is a bit unclear. Also, you should reply to people's questions in the comment to help with clarification. – HangarRash Apr 06 '23 at 15:24
  • @HangarRash, I was not simply asking about enum associated values. Like many many other usefull samples that extend the basic simplicity of enum, I found the text written with the sample to strickt, but it did look for me now more as something similar instead of the "same thing"; that confuses me. On the iPad I can’t hoover over the down or upvote to see more. – iPadawan Apr 07 '23 at 13:50

1 Answers1

0

In Swift 5.3, enum cases gained the ability to be protocol witnesses—this made them special cases of static properties or methods:

  1. A static, get-only protocol requirement having an enum type or Self type can be witnessed by an enum case with no associated values.
  2. A static function requirement with arguments and returning an enum type or Self type can be witnessed by an enum case with associated values having the same argument list as the function's.
protocol Protocol: Equatable {
  static var `var`: Self { get }
  static func `func`(_: String) -> Self
}

enum Enum: Protocol {
  case `var`
  case `func`(String)
}

struct Struct: Protocol {
  static var `var`: Self { .init(string: "") }
  static func `func`(_ string: String) -> Self {
    .init(string: string)
  }

  let string: String
}
let makeEnum = Enum.func
let makeStruct = Struct.func
if case .func(let string) = makeEnum("‍⬛") {
  string == makeStruct("‍⬛").string // true
}

There's no single answer to, "How can I use this?". You can do whatever you like.

  • This makes it more clear thank you. So I will check it out in what way I can do want I need and how I like. Take some time but I will be back. – iPadawan Apr 06 '23 at 13:41