Q:

Unwrap optional properties automatically in enum

Currently I'm using enum to define APIs. One of my api is posting a note, with an image or not.
enum StoreAPI {
    ...
    case newNote(String, Data?) /* note_description, note_image */
}

As I know to handle this case, there are two ways:
// Option 1
switch api {
...
case let newNote(description, imageData):
   if let imageData = imageData {
       // Post with image
   }
   else {
      // Post without image
   }
...
}

// Option 2
switch api {
    ...
    case let newNote(description, nil):
       // Post without image
    case let newNote(description, imageData):
       let imageData = imageData!
    ...
}

I'm wondering if is there any other way to unwrap optional value automatically, or handle it better, or more clearly.

A:

Use optional enum's .some binding:
enum StoreAPI {
    case newNote(String, Data?)
}

// sample data
let api = StoreAPI.newNote("title", "data".data(using: .utf8))

switch api {

case let .newNote(title, .some(data)):
    print("title: \(title), data: \(data)")

default:
    break

}

