21

I created a swift class with string optionals (String?) and instantiated the class in a different swift file and got a compile error. When I instantiate the class within the same file, there is no error. Is there something wrong I am doing? I double checked the behaviour and this behaviour is consistent even with the class definition given in the swift documentation:

class ShoppingListItem {
    var name: String?
    var quantity = 1
    var purchased = false
}
var item = ShoppingListItem()

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/in/jEUH0.l

If the var item = ShoppingListItem() is done in the appDelegate.swift, from the function application:didFinishLaunchingWithOptions we get the error:

<class> cannot be initialised because it has no accessible initializers

OTOH, if we keep the instantiation as soon as the class declaration ends, there is no problem.

Edit: This issue is not a showstopper, the current default initialiser behaviour seems inconsistent or I need to understand it better

7
  • post your error please Commented Aug 15, 2014 at 13:28
  • Also add some context around where you use the classes, I'm not sure I understand the issue. Commented Aug 15, 2014 at 14:15
  • did you add init(){}? Commented Aug 15, 2014 at 14:15
  • @PseudoNym01 there is a compile error: <class> cannot be initialised because it has no accessible initialisers. Commented Aug 15, 2014 at 14:36
  • @HighFlyingFantasy I created a new swift file which would contain my model, within it I created a class (take the code above as the class for a sample). I then instantiated the class within the appDelegate - applicationdidFinishLaunchingWithOptions. Doing this gives a compile error, the error as stated above. Commented Aug 15, 2014 at 14:43

2 Answers 2

32

Chances are it's an issue with the Swift compiler and access control (not pointing fingers, just trying to troubleshoot). Add an explicit initializer to the class and see if that works:

class ShoppingListItem {
    var name: String?
    var quantity = 1
    var purchased = false

    init() { }
}

If that doesn't work, then set the class to public, along with the initializer

    public class ShoppingListItem {
        var name: String?
        var quantity = 1
        var purchased = false

        public init() { }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Yes adding an explicit init() does work and thats what I did, the point of the question was me wanting to be sure that this is an issue with the compiler. From the responses so far it seems like a compiler issue. I would mark this as the answer if there are no other contradicting responses.
It is not a compiler issue. All pure Swift classes (those with no parent), require at least one initializer (or initial values for all their members, including Optionals). This is not very well explained by Apple's documentation.
I am working with this in a Playground and turns out I also needed to make the class, init, and variables public in order to access them. Thanks for the help on this.
Why is it that the Playground requires initializers to be public, but a subclass hierarchy with NSObject as the parent does not?
17

Providing all members with a default value, in this case

var name: String? = nil

fixes the error.

3 Comments

Have just discovered the same
It fixes the error but swift book is saying that "If you define an optional constant or variable without providing a default value, the constant or variable is automatically set to nil for you: ". So, why here do we need to provide explicitly nil?
didn't make a change

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.