Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property definition has inferred type 'some View', involving the 'some' return type of another declaration

Tags:

swiftui

It didn't work when I update to xcode 13 beta 4.

show error:

Property definition has inferred type 'some View', involving the 'some' return type of another declaration

struct RatingToStar : View {
    var rating: Int
    let star = Image(systemName: "star").foregroundColor(.yellow)
    let fillStar = Image(systemName: "star.fill").foregroundColor(.yellow)

    var body: some View {
        HStack {
            if rating >= 1 {fillStar}else{star}
            if rating >= 2 {fillStar}else{star}
            if rating >= 3 {fillStar}else{star}
            if rating >= 4 {fillStar}else{star}
            if rating >= 5 {fillStar}else{star}
        }
    }
}
like image 459
trace tw Avatar asked Jul 21 '19 15:07

trace tw


2 Answers

you can also fix it by specifying a type of some View

let star : some View = Image(systemName: "star").foregroundColor(.yellow)
like image 163
Antoine Weber Avatar answered Nov 11 '22 13:11

Antoine Weber


In beta 4, return types for view modifiers have been cleaned up and made some View. The release notes say so:

View modifier methods return opaque views (some View) rather than complex generic types. (46140669)

This is what's causing the change in behavior. At this time I cannot give you a full explanation on the error message, as I am not entirely confident about it, but I can give you an easy workaround:

struct RatingToSta : View {
    var rating: Int

    let star = AnyView(Image(systemName: "star").foregroundColor(.yellow))
    let fillStar = AnyView(Image(systemName: "star.fill").foregroundColor(.yellow))

    var body: some View {
        HStack {
            if rating >= 1 { fillStar } else{ star }
            if rating >= 2 { fillStar } else{ star }
            if rating >= 3 { fillStar } else{ star }
            if rating >= 4 { fillStar } else{ star }
            if rating >= 5 { fillStar } else{ star }
        }
    }
}

If I find out more, I'll make sure to come back and update my answer.

like image 44
kontiki Avatar answered Nov 11 '22 15:11

kontiki