SWIFT – How do I get the parameter value of enumeration under IF?

See answer in English> Get associated value from enumeration without switch/case 2
How to write this

switch parameter {
case .CaseA(let valueA):
print(valueA)
}

As an If condition statement? This does not work:

if parameter == .CaseA(let valueA) {
print(valueA)
}
If the situation is as follows, you can use

enum Foo { 
case A(Int)
case B(String)
}

let parameter = Foo.A(42)

/* if case ... */
if case .A(let valueA) = parameter {
print(valueA) // 42
}

if case pattern matching is equivalent For switch patterns that match the empty (unused) default case, for example,

/* switch ... */
switch parameter {
case .A(let valueA):
print(valueA) // 42
case _: ()
)

For more information, please refer to the Language Reference – Patterns .

See the answer in English> Get associated value from enumeration without switch/case 2
How to write this

switch parameter {
case .CaseA(let valueA):
print(valueA)
}

As an If condition statement? This does not work:

if parameter == .CaseA(let valueA) {
print(valueA)
}

If the situation is as follows, you can use

enum Foo {
case A(Int)
case B(String)
}

let parameter = Foo.A(42)

/* if case ... */
if case .A(let valueA) = parameter {
print(valueA) // 42
}

If case pattern matching is equivalent to a switch that matches the empty (unused) default case Mode, for example,

/* switch ... */
switch parameter {
case .A(let valueA):
print (valueA) // 42
case _: ()
)

For more information, please refer to the Language Reference – Patterns.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4113 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.