Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift enum error: Braced block of statements is an unused closure

Enums in Swift look to be really powerful, but... I must be missing something about how I'm implementing this. I want to define some actions for a remote media player. Seems like a good use case for an enum. I've defined the allowed message types in the Enum, and I'd like to use it to get a modified parameter dictionary. The parameters will eventually get sent as JSON to the player. At the moment, I'm getting a Braced block of statements is an unused closure error. Here's the relevant code:

public enum PlayerCommand {
case Play
case Pause
case Load(String)

func params(cmd_id:NSInteger) -> [String : Any] {
    var dict = [
        CMD_ID      : cmd_id,
        TYPE        : "LOAD",
        AUTOPLAY    : false,
        MEDIA       : NSNull()
    ]
    switch self {
    case .Load(let media): {
            dict.updateValue(media, forKey: MEDIA)
        }
    case .Play: {
            dict.updateValue("PLAY", forKey: TYPE)
            dict[CURRENT_TIME] = NSNull()
        }
    case .Pause: {
            dict.updateValue("PAUSE", forKey: TYPE)
        }
    default:
    }
    return dict
}
}

I am sure that there is also a more functional (swiftian?) way to express this, as well, but I'm not having a lot of luck with the syntax yet. map?

like image 252
Suz Avatar asked Jan 30 '15 00:01

Suz


People also ask

What is a no escaping closure in Swift?

A closure is said to be no escaping when the closure is passed as an argument to the function, and is called before the function returns. The closure is not used outside of the function. In Swift, all closure parameters are no escaping by default.

What are closures in Swift 4?

Closures in Swift 4 are similar to that of self-contained functions organized as blocks and called anywhere like C and Objective C languages. Constants and variable references defined inside the functions are captured and stored in closures. Functions are considered as special cases of closures and it takes the following three forms − Have a name.

What are closure expressions in Swift?

Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context. Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. These optimizations include: Inferring parameter and return value types from context

How are errors represented in Swift?

In Swift, errors are represented by values of types that conform to the Error protocol. This empty protocol indicates that a type can be used for error handling.


1 Answers

You have your switch syntax a bit off, is all. You don’t need { } around the expressions of each case (Swift is interpreting them as you trying to create a closure expression hence the error).

Just do case .Play: dict.updateValue(etc.).

Note also you must have a statement in the default clause – but you don’t actually need a default in this case, since your switch is exhausting all the possibilities.

like image 178
Airspeed Velocity Avatar answered Oct 02 '22 21:10

Airspeed Velocity