2 Replies
      Latest reply on Apr 18, 2018 5:34 AM by Claude31
      shanshan_misaki Level 1 Level 1 (0 points)

        Hi all,

         

        I just tried to follow the tutorial(https://itunes.apple.com/us/course/developing-ios-10-apps-with-swift/id1198467120) given on swift official website and tried to implement the calculator assignment.

         

        But there is a compile problem in my code.

         

        For "private var brain = CalculatorBrain()" this line, the error message is "Cannot invoke initializer for type 'CalculatorBrain' with no arguments". However, the  CalculatorBrain is a struct. It should be a given free initializer. Why does this problem happen? And how do I fix it?

         

        I did Google this question. But it turned out that most of the problem happened for the class initializer instead of for the struct's. Could you please give me some possible solutions or related resources? So I can try to figure it out.

         

        Thank you so much!

        • Re: Cannot invoke initializer for a struct
          OOPer Level 8 Level 8 (5,215 points)

          It should be a given free initializer.

          NO. Unless the struct is an imported C-struct or all its properties have default value, Swift geneates only memberwise initilizer.

           

          I have never read the course you have shown, but how is the definition of your `CalculatorBrain` now?

           



          • Re: Cannot invoke initializer for a struct
            Claude31 Level 8 Level 8 (6,355 points)

            This is Stanford course (excellent IMHO), not Swift official website.

             

            In the course, CalculatorBrain is a class, not a struct,with the following properties:

             

            class CalculatorBrain {
            
                private var accumulator = 0.0
                private var internalProgram = [AnyObject]()
                var programText = ""
            
            
            

             

            So, you have probably changed it, hence your problem.