code works in playground, but not in project

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"

Accepted Reply

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()
    }

Replies

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()
    }

Thanks Claude31. Your suggestion works for me.

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))

You should better start a new thread of your own issue. A new comment in a solved thread may not get good attention.


Please do not forget to include your code, when starting a new thread.