Last active
February 24, 2022 18:17
-
-
Save mazz/296e7c7a8fb2b020a38c3b1e5bc9bd86 to your computer and use it in GitHub Desktop.
Index out of range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import os.log | |
struct ContentView: View { | |
@StateObject var viewModel: ImageGalleryViewModel = ImageGalleryViewModel() | |
var body: some View { | |
ZStack { | |
TabView { | |
// Text("TabView 1") | |
// .tag(0) | |
// with commented-out lines above, crash Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range | |
ForEach(viewModel.images) { image in | |
Text("\(image.title)") | |
.tag(image.index) | |
} | |
} | |
.tabViewStyle(.page) | |
.indexViewStyle(.page(backgroundDisplayMode: .always)) | |
} | |
.onAppear { | |
viewModel.setupPublishing() | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Combine | |
import UIKit | |
import os.log | |
class ImageGalleryViewModel: ObservableObject { | |
@Published private (set) var images: [GalleryImageModel] = [] | |
} | |
extension ImageGalleryViewModel { | |
func setupPublishing() { | |
let imageNames = ["41345345.242432.jpg", "887987987.242432.jpg", "3453453453.242432.jpg"] | |
var imagesModel: [GalleryImageModel] = [] | |
for (index, imageName) in imageNames.enumerated() { | |
let imageModel = GalleryImageModel(index: index, title: imageName) | |
imagesModel.append(imageModel) | |
} | |
self.images = imagesModel | |
} | |
} | |
extension ImageGalleryViewModel { | |
struct GalleryImageModel: Identifiable { | |
public var id = UUID() | |
var index: Int | |
var title: String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment