4 Replies
      Latest reply on Sep 20, 2018 6:03 PM by OOPer
      ChuckyG Level 1 Level 1 (0 points)

        Hi,

         

        From Chapter 20 of intro to Appl Development with Swift: MemeMaker

         

        This code works fine in playground, but in project's ViewController or separate file, the code seems to be ignored. If I try to type to get autocompletion typing out "topChoice", nothing happens.

         

        No idea why? Anyone got a clue?

         

        struct CaptionOption {

            let emojiOption: String

            let captionphrase: String

        }

        let imCool = CaptionOption(emojiOption: "  ", captionphrase: "I'm cool with that")

        let unlucky = CaptionOption(emojiOption: "  ", captionphrase: "I'm so unlucky")

        let dweeb = CaptionOption(emojiOption: "  ", captionphrase: "You're a dork")

        let run = CaptionOption(emojiOption: "  ♂️", captionphrase: "run away!")

        var topChoice = [CaptionOption]()

        var bottomChoice = [CaptionOption]()

        topChoice.append(imCool) // this will fail: 1) As I type it doesn't autocomplete, and 2) fails with error "expected declaration"

        • Re: code works in playground, but not in project
          Claude31 Level 8 Level 8 (6,865 points)

          I've tried both in playground and in IOS App. Works in both.

           

          But, I've included the code

          topChoice.append(imCool) // this will fail: 1) As I type it doesn't autocomplete, and 2) fails with error "expected declaration"
          

          in a func.

          You cannot put it directly in the class ; in that case, it expects declarations only.

           

          This works

          class ViewController: UIViewController {
              struct CaptionOption {
                  let emojiOption: String
                  let captionphrase: String
              }
            
              override func viewDidLoad() {
                  super.viewDidLoad()
          
                  let imCool = CaptionOption(emojiOption: "  ", captionphrase: "I'm cool with that")
                  let unlucky = CaptionOption(emojiOption: "  ", captionphrase: "I'm so unlucky")
                  let dweeb = CaptionOption(emojiOption: "  ", captionphrase: "You're a dork")
                  let run = CaptionOption(emojiOption: "  ♂️", captionphrase: "run away!")
                  var topChoice = [CaptionOption]()
                  var bottomChoice = [CaptionOption]()
                  topChoice.append(imCool) // this will fail: 1) As I type it doesn't autocomplete, and 2) fails with error "expected declaration"
              }
          
          

           

          This does not

          class ViewController: UIViewController {
              struct CaptionOption {
                  let emojiOption: String
                  let captionphrase: String
              }
          
              let imCool = CaptionOption(emojiOption: "  ", captionphrase: "I'm cool with that")
              let unlucky = CaptionOption(emojiOption: "  ", captionphrase: "I'm so unlucky")
              let dweeb = CaptionOption(emojiOption: "  ", captionphrase: "You're a dork")
              let run = CaptionOption(emojiOption: "  ♂️", captionphrase: "run away!")
              var topChoice = [CaptionOption]()
              var bottomChoice = [CaptionOption]()
              topChoice.append(imCool) // this will fail: 1) As I type it doesn't autocomplete, and 2) fails with error "expected declaration"
          
              override func viewDidLoad() {
                  super.viewDidLoad()
              }
          
          
          • Re: code works in playground, but not in project
            Vlad_tsvetkov Level 1 Level 1 (0 points)

            Hello, I have the same problem.
            In my progect don't work automatically generated memberwise initializer for structs and I can't understand why
            But it work correctly in playground
            Can you help me ?

            I use betta Mac OS and betta XCode ( Version 10.0 (10A254a))