12

For my SwiftUI application, I've created a simple Title view, that has a set font size and text color. Title is declared as follows:

struct Title: View {
    var string: String
    
    var body: some View {
        Text(string)
            .font(.system(size: 32))
            .color(Color.black)
    }
}

I have the following text objects in my content view's body right now:

var body: some View {
    VStack(alignment: .leading) {
        Text("Welcome")
            .font(.largeTitle)
            .color(Color.black)
        Text("to SwiftUI")
            .font(.largeTitle)
            .color(Color.secondary)
    }
}

So now, I want to replace these two Texts with my Titles:

var body: some View {
    VStack(alignment: .leading) {
        Title("Welcome")
        Title("to SwiftUI")
    }
}

After replacing the views, I'm getting some seemingly unrelated error messages from Xcode, that stop the application from compiling:

Static member 'leading' cannot be used on instance of type 'HorizontalAlignment'

'(LocalizedStringKey) -> Text' is not convertible to '(LocalizedStringKey, String?, Bundle?, StaticString?) -> Text'

'Font' is not convertible to 'Font?'

...and more. Reverting back to Text instead of Title "fixes" the issues.

What's interesting is that I also have a custom PrimaryButton view that I was able to add without any issues:

struct PrimaryButton: View {
    var title: String
    
    var body: some View {
        Button(action: { print("tapped") }) {
            Text(title)
                .font(Font.primaryButton)
                .offset(y: 1)
                .padding(.horizontal, 20)
                .padding(.vertical, 14)
        }
    }
}

...and then using it:

PrimaryButton(title: "Let's go")

Question

Is this simply a beta-issue, or am I missing something?

3 Answers 3

13

You need to add string: to your Title() initializer:

var body: some View {
    VStack(alignment: .leading) {
        Title(string: "Welcome")
        Title(string: "to SwiftUI")
    }
}

Compiler errors are currently misleading and not located near where the real issue is.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh I did not spot that, thanks! For leaving it out, do I have to write a custom initializer?
You need to provide values for all struct values when creating one, but you don't have to explicitly write a custom initializer. But as of Swift 5.1 (I believe), you only have to provide values for those members which are not initialized with default values.
3

You are missing the string: param in the initializer. Please find the updated code below:

var body: some View {
    VStack(alignment: .leading) {
        Title(string: "Welcome")
        Title(string: "to SwiftUI")
    }
}

FYI:
I have created one sample application

// MARK - CustomView

struct ContentView : View {
    var body: some View {
        VStack{
            CustomView(aString: "First String")
            CustomView(aString: "Second String")
        }
    }
}

// MARK - CustomView

struct CustomView : View {
    var aString: String
    var body: some View {
        Text(aString)
    }
}

2 Comments

Is it possible to do the same with TextField instead of Text Views? How do I get the value of the @State var for the TextField from the View that is using it?
Can you share the link to your sample application
0

Today, 01oct2019, Swift prompted me to replace string: with. verbatim: .

Text(verbatim: "Pressure") works today Text(string: "Pressure") did work yesterday but not today. hth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.