UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

What am I doing wrong with func ?

Forums > SwiftUI

@GMacD  

I am completely stuck with this! In desperation I have copied and pasted several examples from the web with a func in them and none have worked. I should explain that I am an absolute newbie and to add to my disability I am doing this on an iPad in Playgrounds. Can someone please pick the bones out of this and tell me where I am going wrong. The Error: Expected 'func' keyword in instance method declaration is at the end of this code. Also I have // a couple of questions about my use of func (See first two funcs) //

import SwiftUI

// var formArray = [String]()
// var gameArray = [String]()

struct FormArrayBuilderView: View{

    @ObservedObject var cards = Cards()
    @State var firstCards = (String)()
    @State var secondCards = (String)()
    @State var thirdCards = (String)()
    @State var fourthCards = (String)()
    @State var fifthCards = (String)()
    @State var sixthCards = (String)()
    @State var formData = (String)()

    var body: some View {

        VStack {

            /* This is where the created Form Data (formData) will be placed into (formArray) and then change (gameArray) to equal (formArray) in another View for use by the Game Cards */

            Text("This is where the created Form Data (formData) will be placed into (formArray) and then change (gameArray) to equal (formArray) in another View for use by the Game Cards")
                .font(.body)
                .foregroundColor(.black)
                .frame(alignment: .center)

            Text(formData) // Not working (functions not working) //
                .font(.body)
                .foregroundColor(.black)
                .frame(alignment: .center)
        }
    }
    func joinCards() {
        firstCardsJoined() // Is this okay? (Calling a func inside a func) //
    }
    func firstCardsJoined() { // Is this okay? (Building a String inside func this way) //
        firstCards = (cards.firstCardNumber + cards.firstCardFirstWordClue + cards.firstCardSecondWordClue + cards.firstCardThirdWordClue + cards.firstCardAnswer + cards.firstCardFirstLetters + cards.firstCardSecondLetters + cards.firstCardThirdLetters)
        secondCardsJoined()
    }
    func secondCardsJoined() {
        secondCards = (cards.secondCardNumber + cards.secondCardFirstWordClue + cards.secondCardSecondWordClue + cards.secondCardThirdWordClue + cards.secondCardAnswer + cards.secondCardFirstLetters +  cards.secondCardSecondLetters + cards.secondCardThirdLetters)
        thirdCardsJoined()
    }
    func thirdCardsJoined() {
        thirdCards = (cards.thirdCardNumber + cards.thirdCardFirstWordClue + cards.thirdCardSecondWordClue + cards.thirdCardThirdWordClue + cards.thirdCardAnswer + cards.thirdCardFirstLetters + cards.thirdCardSecondLetters + cards.thirdCardThirdLetters)
        fourthCardsJoined()
    }
    func fourthCardsJoined() {
        fourthCards = (cards.fourthCardNumber + cards.fourthCardFirstWordClue + cards.fourthCardSecondWordClue + cards.fourthCardThirdWordClue + cards.fourthCardAnswer + cards.fourthCardFirstLetters + cards.fourthCardSecondLetters + cards.fourthCardThirdLetters)
        fifthCardsJoined()
    }
    func fifthCardsJoined() {
        fifthCards = (cards.fifthCardNumber + cards.fifthCardFirstWordClue + cards.fifthCardSecondWordClue + cards.fifthCardThirdWordClue + cards.fifthCardAnswer + cards.fifthCardFirstLetters + cards.fifthCardSecondLetters + cards.fifthCardThirdLetters)
        sixthCardsJoined()
    }
    func sixthCardsJoined() {
        sixthCards = (cards.sixthCardNumber + cards.sixthCardFirstWordClue + cards.sixthCardSecondWordClue + cards.sixthCardThirdWordClue + cards.sixthCardAnswer + cards.sixthCardFirstLetters + cards.sixthCardSecondLetters + cards.sixthCardThirdLetters)
        allCardsJoined()
    }
    func allCardsJoined() {
        formData = (firstCards + secondCards + thirdCards + fourthCards + fifthCards + sixthCards)
    }
    //////////////////////////////////////////////////////////////////////
    joinCards()
    // Error: Expected 'func' keyword in instance method declaration
    //////////////////////////////////////////////////////////////////////
}

Kind Regards

Guy MacDonald

1      

@Bnerd  

You want to call the joinCards() when the view appears? , if yes add at the bottom of the Vstack

.onAppear {
joinCards()
}

and remove it from where you have it.

1      

@GMacD  

Thank you

That has got rid of the error but I still have a problem. The App (in Playgrounds) is now crashing (BST Transaction Error (1):....Process failed to launch....) I will try trouble shooting my code which I suspect is flawed!

Thanks again

Guy

1      

@GMacD  

Hello again

It seems that.....

.onAppear {

}

is causing the App to crash.

Looking for suggestions for what I can put inside .onAppear to test it without any of my code.

Thanks again

Guy

1      

@GMacD  

Hello again

I feel very silly!!

I didn't syntax it correctly!!

.onAppear(perform: { joinCards() })

Is what it should have been!!

Thanks again

Guy

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.