Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumeration: "cannot be constructed because it has no accessible initializers"

Tags:

swift

I can't initialize an enumeration using the rawValue initializer. Any thoughts? Error commented below:

//: Playground - noun: a place where people can play
// Xcode Version 7.3 (7D175)

import UIKit

enum Theme {
    case Default, Dark, Graphical
}

let possibleTheme = Theme(rawValue: 1) 
// Error: 'Theme' cannot be constructed because it has no accessible initializers.
like image 443
Ryan Huebert Avatar asked Mar 23 '16 18:03

Ryan Huebert


1 Answers

enum Theme: Int {
    case Default, Dark, Graphical
}

let possibleTheme = Theme(rawValue: 1) // Dark

An enum doesn't have a raw value unless you specify its type. Possible raw value types are String, Character and any of the number types. Documentation

like image 77
Tim Vermeulen Avatar answered Oct 15 '22 12:10

Tim Vermeulen