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
Dynamictype of Optional Chaining Not The Same as Assignment
Swift Getnameinfo Unreliable Results for Ipv6
Why Does User Defaults Publisher Trigger Multiple Times
Can't Override UItableviewdatasource and UItableviewdelegate
Swift Flatmap on Array with Elements Are Optional Has Different Behavior
How to Find Multiple Nsrange for a String from Full String iOS Swift
Sharing Button Works Perfectly on iPhone But Crash on Ipad
Swiftui Custom View Repeat Forever Animation Show as Unexpected
How to Avoid Parent Scrollview to Clip Internal Scrollview
Siblings Relationship Between Same Models in Vapor
How to Query Firebase Data Childbyautoid
How to Determine If a Year Has a Leap Month in Swift
Setadvertisertrackingenabled Is Not Found in Settings Class of Fbsdk
Convert Input Data to Integer in Swift
Implement an Equatable Void (None) Type
How to Navigate from Initial UIviewcontroller to UIsplitviewcontroller in Swift