Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No exact matches in call to initializer of Text, how can I solve this issue in SwiftUI?

Tags:

swift

swiftui

I am new to swift, this might be a stupid question but it confuses me a long time. Here is my code, I wrote this code by imitating the sandwich example from WWDC 2020's intro to swiftUI.

import SwiftUI

struct movieDetail: View {

    var idnum:Int

    var body: some View {

        Text(idnum)

    }
}

struct movieDetail_Previews: PreviewProvider {

    static var previews: some View {

        NavigationView {
            movieDetail(idnum:24428)
        }
       
    }
}

while it displays as compile error:

No exact matches in call to initializer.

WWDC 2020's example is below

import SwiftUI

struct sandwichdetail: View {

    var sandwich:Sandwich
    @State private var zoomed = false

    var body: some View {

        VStack {
            Text(sandwich.name)
        }
        .navigationTitle(sandwich.name)
        .edgesIgnoringSafeArea(.bottom)
    }
}

struct sandwichdetail_Previews: PreviewProvider {

    static var previews: some View {
        NavigationView{
            sandwichdetail(sandwich: testData[1])
        }
        
    }
}

I don't quite understand the difference between these 2 pieces of code and why mine fail and the sandwich example succeed displaying the sandwich's name.

like image 938
680680 Avatar asked Oct 31 '25 05:10

680680


2 Answers

The Text type doesn't have an initializer that takes an Int. Try this instead:

Text("\(idnum)")
like image 196
rob mayoff Avatar answered Nov 01 '25 20:11

rob mayoff


Another more Swifty way, is using description because Int conforms to CustomStringConvertible protocol.

Text(idnum.description)
like image 39
ios coder Avatar answered Nov 01 '25 21:11

ios coder