Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

swift3

Not sure if this is an error in my code or a glitch in XCode.

I have this class (simplified version for the sake of clarity)

public class Error {

    let errors: [ (title: String, message: String)] =
        [("Some error title","Some error message"),
         ("Another error title", "Another error message") 
        ]

    var errorNo : Int

    init (_ errorNo: Int) {

        self.errorNo = errorNo
    }

    func title () -> String {
        return self.errors[self.errorNo].title
    }

    func message () -> String {
        return self.errors[self.errorNo].message
    }
}

In another class I have

if someCondition {
    return Error (0)
}

Now the strange thing... Everything compiles and runs but if I let XCode sit idle for a few moments (not the fastest computer I'm using), XCode is giving me the infamous red dots (with exclamation marks) with the error:

'Error' cannot be constructed because it has no accessible initialisers

next to each time I do Error(0) (whatever Int I use in the constructor)

I can compile and run again and the errors dissappear and then reappear

Using XCode version 8.1 (8B62)

******** SEE COMMENTS **** additional info ********

Still one (similar problem now after renaming Error to AppError)

func doSomething (blah: Int, test : String) -> AppError {

    some code
    return AppError(1)
}

It compiles and runs but after some time an error pops up next to func doSomething

Use of undeclared type 'AppError'

like image 219
Glenn Avatar asked Nov 10 '16 10:11

Glenn


1 Answers

Error is a Swift protocol and therefore has no accessible initialisers. It is possible your compiler is confusing Swift.Error with your local definition of Error. When referring to your Error type to avoid confusion you should include the namespace i.e.

(Target Name).Error

Regarding the errors you have seen after renaming your class to AppError, there is an XCode bug where it displays old errors after the app compiles and runs, as long as it is compiling and running you can ignore these errors.

like image 65
Kerstin Avatar answered Nov 12 '22 02:11

Kerstin