Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : Enum 'cannot be constructed because it has no accessible initializers'

Tags:

enums

swift

I get this Error in swift

'BlockColor' cannot be constructed because it has no accessible initializers

import Foundation
import SpriteKit

let NumberOfColors: UInt32 = 6

enum BlockColor: Int, Printable {

case Blue = 0, Orange, Purple, Red, Teal, Yellow

var spriteName: String {
    switch self {
    case .Blue:
        return "blue"
    case .Orange:
        return "orange"
    case .Purple:
        return "purple"
    case .Red:
        return "red"
    case .Teal:
        return "teal"
    case .Yellow:
        return "yellow"
        }
}

var description: String {
    return self.spriteName
}

static func random() -> BlockColor {
    return BlockColor(rawValue:Int(arc4random_uniform(NumberOfColors)))!
}
}

I got an error in this line

 return BlockColor(rawValue:Int(arc4random_uniform(NumberOfColors)))!

I have review my code many times but I couldn't find where is the error

like image 399
DF2015 Avatar asked Mar 06 '15 18:03

DF2015


1 Answers

I got the same error. My mistake was that i didn't mention any return (Int) type for enum method ( enum BlockColor:Int). after initialization of Int return type. Its Works now.

like image 200
Prabha Palanisamy Avatar answered Jan 02 '23 23:01

Prabha Palanisamy