1

I know a little about how to use convenience initializer and designated initializer.Here is a sample class called ClassA

class ClassA {
    var number:Int

    convenience init(){
        self.init(newNumber: 10)
    }

    init(newNumber: Int) {
        self.number = newNumber;
    }
}

This class works well but I got some trouble when implementing another class called Base. Here is the code:

class Base {
    var classObject:ClassA

    init(object: ClassA) {
        self.classObject = object
    }

    convenience init(){
        self.init(object: ClassA.init())
    }
}

In the convenience initializer of class Base, I call the convenience initializer of ClassA and take it as the argument of the designated initializer of Base.

However, I get two syntax errors.

  1. Expected member name or constructor call after type name.
  2. () is not convertible to Class A.

If I use

self.init(object: ClassA.init(newNumber: 10))

instead of

self.init(object: ClassA.init())

Then the first error disappears. It seems that the convenience initializer of ClassA is not recognized.

sevenkplus
  • 178
  • 1
  • 12
  • Your code compiles perfectly for me. Perhaps you've hit a bug in an earlier version of Swift? Update to Swift 2.0 and try again. Voting to close as irreproducible. – matt Aug 18 '15 at 16:52
  • I am using Xcode 6.4. Should I update it to 7.0? – sevenkplus Aug 18 '15 at 16:57
  • @matt It does not compile correctly. `ClassA.init()` returns void. You should use `ClassA()` – Roger Aug 18 '15 at 17:08
  • It *is* reproducible with Xcode 6.4. – Martin R Aug 18 '15 at 17:28
  • @MartinR If it's a bug in Xcode 6.4 and it's fixed in Xcode 7, then it's a fixed bug. — However, @Roger is also right: use of explicit `init()` should be confined to things like `self.init()`. – matt Aug 18 '15 at 17:33

1 Answers1

1

try to use self.init(object: ClassA())

Roger
  • 973
  • 10
  • 15
  • I don't know who has down voted for what reason. You should leave a message at least? – Roger Aug 18 '15 at 17:04
  • This is ok. I have tried to do so before. But why doesn't my code work? There is no error if I use `var object = ClassA.init()` outside the definition of class Bae – sevenkplus Aug 18 '15 at 17:13
  • @sevenkplus As I said in the comment. `ClassA.init()` returns `void` instead of an instance of the object. You should use ClassA(). Maybe you should be using that in the Objective-C class which has a class method named `init` – Roger Aug 18 '15 at 17:17
  • In your code `ClassA.init()` returns `Void`, the () in you error which is not convertible to `Class A`. So you can use this outside. And if you print it out `println(object)`, you will see the magic – Roger Aug 18 '15 at 17:32
  • This can explain why I got an error.But as you said, I used both ways to create an object outside and print it out. Both of the output is in the format of PROJECT_NAME.CLASS_NAME. – sevenkplus Aug 19 '15 at 15:38
  • @sevenkplus seems it's a bug – Roger Aug 23 '15 at 09:03
  • So, in conclusion, I should always user ClassName() instead of ClassName.init() to create an instance of a class unless it's in the convenience initializer.Right? – sevenkplus Aug 23 '15 at 17:02