Problem with Swiftui and Foreach on Xcode Playground

Problem with SwiftUI and foreach on Xcode playground

It seems there is a bug with (at least this version) of Playground, when using ForEach. I have the same issue and you can find more details in the CrashLogs of console

Check crashing playground with ForEach

Workarround

  • Move ContentView to a separate file in Sources of Playground
  • Don't forget the public modifiers

public struct ContentView: View {

let data = (1...100).map { "Item \($0)" }

let columns = [
GridItem(.adaptive(minimum: 80))
]

public init() {}

public var body: some View {
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(data, id: \.self) { item in
Text(item)
}
}
.padding(.horizontal)
}
.frame(maxHeight: 300)
}
}

problem in Xcode playground with SwiftUI , Xcode 12.4

In your attached image, you show some code that you didn't actually include in your question. In it, you initialize a UIHostingController with a certain size, but then never do anything with it -- instead, you set the playground view to just ContentView.

You can make the ContentView appear with a larger frame by adding a frame modifier to it:

PlaygroundPage.current.setLiveView(ContentView().frame(width: 375, height: 667))

Issues with ForEach Loop over view model array causing Xcode compile error - SwiftUI

Based on the comments above by loreipsum and George, my issue was that GroupCellViewModel didn't conform to Identifiable. I fixed this and that solved the issue:

class GroupCellViewModel: ObservableObject, Identifiable {

@Published var groupRepository: GroupStoreType

@Published var group: AccountabilityGroup

private var cancellables = Set<AnyCancellable>()

init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared, accountabilityGroup: AccountabilityGroup) {
self.groupRepository = groupRepository
self.group = accountabilityGroup
}

}

Wrong counting while using ForEach in SwiftUI

You just have to set

coffeAmmount

to 0:

@State private var coffeAmmount = 0

SwiftUI treats this variable as a pointer to the place in range you created with use of ForEach.
So the ForEach range is (1, 2, 3, ..., 20) but their indexes are 0 for 1; 1 for 2 etc. Swift, as many of the programming languages, starts counting the indexes and other stuff from 0 not from 1 by default.

By setting coffeAmmount to 0 you actually show Xcode that you want the value from the index (place in range you created) "branded" 0 which value is 1 :)

Hope that helps people with the similar problem, since the question was asked 2 years ago.

SwiftUI Simple ForEach multiple errors does not conform to View

The ForEach is a view container, so inside it there should be some view, but your net is a model (instance of SocialNetwork). Put there some list row view presenting one network, like

        List {
ForEach(networks) { net in
Label(net.url, image: net.icon)
}
}

Problem with ForEach and NavigationLink in SwiftUI

It has something to do with the shorthand initializers that you are using. Either of these alternatives will work:

ForEach(pets, id: \.self) {
NavigationLink($0, destination: Text($0))
}

ForEach(pets, id: \.self) { pet in
NavigationLink(destination: Text(pet)) {
Text(pet)
}
}


Related Topics



Leave a reply



Submit