Swift: Enum 'cannot be constructed because it does not have initializers available'

I get this error in quick

'BlockColor' cannot be constructed because initializers are not available

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 on this line

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

I look through my code many times but have not found where the error is

+6
source share
2 answers

I got the same error. My mistake was that I did not mention the return type (Int) for the enum method (BlockColor: Int enumeration). after initializing the type of the return type. His work is now.

+9
source

the problem is resolved :) it looks like the problem was in my Xcode 6.0. The code works fine in Xcode 6.2 beta.

0
source

Source: https://habr.com/ru/post/983438/


All Articles