Error: Initializers may only be declared within a type

Sahil Kapoor

Here's my code

extension UIImage {
    convenience init(color: UIColor, size: CGSize = CGSizeMake(1, 1)) {
        let rect = CGRectMake(0, 0, size.width, size.height)
        UIGraphicsBeginImageContext(rect.size)

        let context = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        init(CGImage: image.CGImage!)
    }
}

On init(CGImage: image.CGImage!), I get the error

Initializers may only be declared within a type

Martin R

The dedicated initializer is called from the convenience initializer with self.init(...):

self.init(CGImage: image.CGImage!)

Without self. (or super. if you call the superclass initializer) the compiler mistakes init(...) for the declaration of an init method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Error: Only static members can be accessed in initializers what does this mean?

From Dev

Are there any Cocoa initializers that may be used to describe a person?

From Dev

Error: a type annotation may be needed

From Dev

Error "An array may not have elements of this type"

From Dev

"The method main cannot be declared static; static methods can only be declared in a static or top level type"

From Dev

error: new types may not be defined in a return type

From Dev

How to get around 'Only Parameterless constructors and initializers...' error

From Dev

ERROR: return type mismatch in function declared to return

From Dev

Swift: class methods may only be declared on a type

From Dev

Symfony2: Error "Property was already declared, but it must be declared only once"

From Dev

Functions may be declared only at top level in strict mode

From Dev

XSD Error: Type is not declared, or is not a simple type

From Dev

Struct can be declared within method body, but only if it doesn't contain member field initializers. Compiler bug or not?

From Dev

Are there any Cocoa initializers that may be used to describe a person?

From Dev

"The method main cannot be declared static; static methods can only be declared in a static or top level type"

From Dev

error: new types may not be defined in a return type

From Dev

Action has no declared type error

From Dev

The method main cannot be declared static; static methods can only be declared in a static or top level type

From Dev

Only parameterless constructors and initializers are supported in LINQ to Entities error while doing version check

From Dev

ERROR: return type mismatch in function declared to return

From Dev

Symfony2: Error "Property was already declared, but it must be declared only once"

From Dev

How to show an error in the Xtext IDE if a type is referenced before it is declared

From Dev

Implicit type compilation error although declared

From Dev

"variable is accessed from within inner class needs to be declared final" error

From Dev

Initializers may only be declared within a type / coder

From Dev

"A case label may only be used within a switch"

From Dev

Initializers may only be declared within a type swift 3

From Dev

Error: Only parameterless constructors and initializers are supported in LINQ to Entities

From Dev

Does not name type and was not declared error

Related Related

  1. 1

    Error: Only static members can be accessed in initializers what does this mean?

  2. 2

    Are there any Cocoa initializers that may be used to describe a person?

  3. 3

    Error: a type annotation may be needed

  4. 4

    Error "An array may not have elements of this type"

  5. 5

    "The method main cannot be declared static; static methods can only be declared in a static or top level type"

  6. 6

    error: new types may not be defined in a return type

  7. 7

    How to get around 'Only Parameterless constructors and initializers...' error

  8. 8

    ERROR: return type mismatch in function declared to return

  9. 9

    Swift: class methods may only be declared on a type

  10. 10

    Symfony2: Error "Property was already declared, but it must be declared only once"

  11. 11

    Functions may be declared only at top level in strict mode

  12. 12

    XSD Error: Type is not declared, or is not a simple type

  13. 13

    Struct can be declared within method body, but only if it doesn't contain member field initializers. Compiler bug or not?

  14. 14

    Are there any Cocoa initializers that may be used to describe a person?

  15. 15

    "The method main cannot be declared static; static methods can only be declared in a static or top level type"

  16. 16

    error: new types may not be defined in a return type

  17. 17

    Action has no declared type error

  18. 18

    The method main cannot be declared static; static methods can only be declared in a static or top level type

  19. 19

    Only parameterless constructors and initializers are supported in LINQ to Entities error while doing version check

  20. 20

    ERROR: return type mismatch in function declared to return

  21. 21

    Symfony2: Error "Property was already declared, but it must be declared only once"

  22. 22

    How to show an error in the Xtext IDE if a type is referenced before it is declared

  23. 23

    Implicit type compilation error although declared

  24. 24

    "variable is accessed from within inner class needs to be declared final" error

  25. 25

    Initializers may only be declared within a type / coder

  26. 26

    "A case label may only be used within a switch"

  27. 27

    Initializers may only be declared within a type swift 3

  28. 28

    Error: Only parameterless constructors and initializers are supported in LINQ to Entities

  29. 29

    Does not name type and was not declared error

HotTag

Archive