swift - Instantiating a struct yields "cannot be constructed because it has no accessible initializers" -


the problem

i have following struct represents rgba pixel. instantiate create pixel object of own i'm failing so. compiler throwing error 'pixel' cannot constructed because has no accessible initializers

what tried

for example, these return same error:

var mypixel = pixel() var mypixel = pixel(value: uint32(1)) var mypixel = pixel(value: uint32(1), red: uint8(1), green: uint8(1), blue: uint8(1)) 

the struct in question

public struct pixel {     public var value: uint32      public var red: uint8 {         {             return uint8(value & 0xff)         }         set {             value = uint32(newvalue) | (value & 0xffffff00)         }     }      public var green: uint8 {         {             return uint8((value >> 8) & 0xff)         }         set {             value = (uint32(newvalue) << 8) | (value & 0xffff00ff)         }     }      public var blue: uint8 {         {             return uint8((value >> 16) & 0xff)         }         set {             value = (uint32(newvalue) << 16) | (value & 0xff00ffff)         }     }      public var alpha: uint8 {         {             return uint8((value >> 24) & 0xff)         }         set {             value = (uint32(newvalue) << 24) | (value & 0x00ffffff)         }     } } 

how other people use in same code base

i cannot change struct being used someplace else , working. i've dug through code use in pixel array cannot make heads or tails of (first time using swift). here's think relevant bits:

public struct rgbaimage {     public var pixels: [pixel]     // ...         public init?(image: uiimage) {         // ...         let imagedata = unsafemutablepointer<pixel>.alloc(width * height)         // ...         let bufferpointer = unsafemutablebufferpointer<pixel>(start: imagedata, count: width * height)         pixels = array(bufferpointer)         // ...     } } 

edit:

i'm using swift playground , pixel struct , rgbaimage struct in separate file in sources folder. code not working in main playground page. if copy/paste pixel struct main page, works expected.


Comments