SwiftUI ForEach Type '_' has no member 'id'

krjw

When I use a custom view in a ForEach I get Type '_' has no member 'id' error. When I use Text(item.x) instead of the custom view it compiles and works. What am I missing?

@State private var showTargets = [
    (id: 1, state: false, x: 109.28, y: 109.28),
    (id: 2, state: false, x: 683, y: 109.28),
    (id: 3, state: false, x: 1256.72, y: 109.28)
]

...

var body: some View {
    Group {

        ForEach(showTargets, id: \.id) { item in
            Text(String(item.x))
            // Using CustomView(x: item.x, y: item.y, f: {}) instead does not work
        }
}

Custom View:

struct CustomView : View {

    @State private var color = Color.white
    @State private var animate = false


    internal var x: CGFloat
    internal var y: CGFloat
    internal var f: ()->()
    internal let w: CGFloat = 60
    internal let h: CGFloat = 60

    private let width = -1366/2
    private let height = -1024/2

    var body: some View {
        Button(action: {
            self.animate.toggle()
            if self.color == Color.green {
                self.color = Color.white
            }
            else {
                self.color = Color.green
                self.f()
            }
        }, label: {
            Ellipse()
                .fill(self.color)
                .scaleEffect(self.animate ? 1.2 : 1)
                .animation(Animation.easeInOut)

        }).position(x: self.x + self.w/2, y: self.y + self.h/2)
            .frame(width: self.w, height: self.h, alignment: .center)
            .offset(CGSize(width: self.width, height: self.height))

    }
}
rraphael

You are trying to initialize your CustomView with the wrong types (Double instead of CGFloat).

Your CustomView initializer looks like this:

init(x: CGFloat, y: CGFloat, f: () -> Void)

And you call it using the showTargets tuple values, which is:

(id: Int, state: Bool, x: Double, y: Double)

So when you do this:

CustomView(x: item.x, y: item.y, f: {})

You are providing Double values (for x and y) instead of CGFloat. As the cast from Double to CGFloat cannot be implicit, you need to do it explicitly:

CustomView(x: CGFloat(item.x), y: CGFloat(item.y), f: {})

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Type '_' has no member 'id' SwiftUI

分類Dev

Type 'UIImage' has no member 'radialGradientImage'

分類Dev

Value of type 'StorageMetadata' has no member 'downloadURL'

分類Dev

Value of type 'StorageMetadata' has no member 'downloadURL'

分類Dev

Type 'NSNotification.Name?' has no member 'firInstanceIDTokenRefresh'

分類Dev

Value of type 'UICollectionViewCell' has no member 'page'

分類Dev

Value of type AuthDataResult has no member providerID

分類Dev

Value of type 'SecondPageController' has no member 'miniView'

分類Dev

Value of type 'DetailViewController' has no member 'word'

分類Dev

Value of type 'X' has no member 'y' - optional func in protocol

分類Dev

IOS FoodTracker Tutorial: Value of tuple type '()' has no member 'path'

分類Dev

Swift: Alamofire Value of type 'Self' (Request) has no member 'responseJSON' extension

分類Dev

Why is it allowed to have a class that has an incomplete type static member of itself?

分類Dev

cv has no member BackgroundSubtractorMOG

分類Dev

Class has no member "Class"

分類Dev

SwiftUI-ForEach with Stride

分類Dev

Using enumerated with ForEach in SwiftUI

分類Dev

Problem with ForEach and NavigationLink in SwiftUI

分類Dev

Xcode 9.4.1 Compiler error saying that Value of type ‘Int’ has no member ‘rawValue’

分類Dev

SwiftUI ForEach'identified(by :) 'は非推奨です。ForEach(_:id :)またはList(_:id :)を使用します

分類Dev

TypeScript | Module has no exported member

分類Dev

Class "classname" has no member "function"

分類Dev

Module turtle has no write member

分類Dev

SwiftUIの@BindingとForEach

分類Dev

Cannot compile SwiftUI ForEach Table

分類Dev

Xcode giving error Value of type 'FlutterViewController' has no member 'binaryMessenger' when writing ios native swift code in flutter project

分類Dev

Value * is not a member of type parameter Int

分類Dev

Override member type in child interface

分類Dev

MSVS 2015: vector<bool> has no 'data' member

Related 関連記事

  1. 1

    Type '_' has no member 'id' SwiftUI

  2. 2

    Type 'UIImage' has no member 'radialGradientImage'

  3. 3

    Value of type 'StorageMetadata' has no member 'downloadURL'

  4. 4

    Value of type 'StorageMetadata' has no member 'downloadURL'

  5. 5

    Type 'NSNotification.Name?' has no member 'firInstanceIDTokenRefresh'

  6. 6

    Value of type 'UICollectionViewCell' has no member 'page'

  7. 7

    Value of type AuthDataResult has no member providerID

  8. 8

    Value of type 'SecondPageController' has no member 'miniView'

  9. 9

    Value of type 'DetailViewController' has no member 'word'

  10. 10

    Value of type 'X' has no member 'y' - optional func in protocol

  11. 11

    IOS FoodTracker Tutorial: Value of tuple type '()' has no member 'path'

  12. 12

    Swift: Alamofire Value of type 'Self' (Request) has no member 'responseJSON' extension

  13. 13

    Why is it allowed to have a class that has an incomplete type static member of itself?

  14. 14

    cv has no member BackgroundSubtractorMOG

  15. 15

    Class has no member "Class"

  16. 16

    SwiftUI-ForEach with Stride

  17. 17

    Using enumerated with ForEach in SwiftUI

  18. 18

    Problem with ForEach and NavigationLink in SwiftUI

  19. 19

    Xcode 9.4.1 Compiler error saying that Value of type ‘Int’ has no member ‘rawValue’

  20. 20

    SwiftUI ForEach'identified(by :) 'は非推奨です。ForEach(_:id :)またはList(_:id :)を使用します

  21. 21

    TypeScript | Module has no exported member

  22. 22

    Class "classname" has no member "function"

  23. 23

    Module turtle has no write member

  24. 24

    SwiftUIの@BindingとForEach

  25. 25

    Cannot compile SwiftUI ForEach Table

  26. 26

    Xcode giving error Value of type 'FlutterViewController' has no member 'binaryMessenger' when writing ios native swift code in flutter project

  27. 27

    Value * is not a member of type parameter Int

  28. 28

    Override member type in child interface

  29. 29

    MSVS 2015: vector<bool> has no 'data' member

ホットタグ

アーカイブ