Ad
How To Use A Generic Array - SwiftUI
Hi I'm creating a component that needs to consider an array for its operation.
I created a model for displaying the array like this
struct FeaturedWorksModel: Identifiable {
var id = UUID()
var image: String
var title: String
}
var featuredWorksItems: [FeaturedWorksModel] = [
FeaturedWorksModel(image: "slider1", title: "TAGLIO CREATIVO"),
FeaturedWorksModel(image: "slider4", title: "LOREM IPSUM"),
FeaturedWorksModel(image: "slider3", title: "NEQUE EST DOLOR"),
FeaturedWorksModel(image: "slider2", title: "CONSECTETUR, INTEGER ERAT AUGUE ")
]
Now my component can also be used with other arrays and I wanted to know how I can define a generic array within the component initialization parameters in order to reuse the component even referring to arrays other than the one I am using in this moment
This is the component I am using to which I will have to pass the reference array for it to work
struct SmoothPager: View {
@Binding var offset: CGPoint
var body: some View {
HStack(spacing: 10) {
ForEach(featuredWorksItems.indices) { works in
Capsule()
.fill(getIndex() == works ? .white : .gray)
.frame(width: getIndex() == works ? 15 : 7, height: 7)
}
}
.overlay(
Capsule()
.fill(.white)
.frame(width: 15, height: 7)
.offset(x: getOffset())
, alignment: .leading
)
}
func getIndex()-> Int {
let index = Int(round(Double(offset.x / screen.width)))
return index
}
func getOffset()-> CGFloat {
let progress = offset.x / screen.width
return 17 * progress
}
}
In this case I am using featuredWorksItems.indices
but I wanted to make the component reusable with other similar arrays as well. What is the best way to pass a generic array parameter in this structure?
Ad
Answer
Do you mean something like
struct SmoothPager<Element: Identifiable>: View {
@Binding private var offset: CGPoint
private let elements: [Element]
init(elements: [Element], offset: Binding<CGPoint>) {
self.elements = elements
self._offset = offset
}
var body: some View {
HStack(spacing: 10) {
ForEach(elements.indices) { works in
Capsule()
.fill(getIndex() == works ? .white : .gray)
.frame(width: getIndex() == works ? 15 : 7, height: 7)
}
}
.overlay(
Capsule()
.fill(.white)
.frame(width: 15, height: 7)
.offset(x: getOffset())
, alignment: .leading
)
}
func getIndex()-> Int {
let index = Int(round(Double(offset.x / UIScreen.main.bounds.width)))
return index
}
func getOffset()-> CGFloat {
let progress = offset.x / UIScreen.main.bounds.width
return 17 * progress
}
}
Ad
source: stackoverflow.com
Related Questions
- → Function Undefined in Axios promise
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Click to navigate on mobile devices
- → Playing Video - Server is not correctly configured - 12939
- → How to allow api access to android or ios app only(laravel)?
- → Axios array map callback
- → Access the Camera and CameraRoll on Android using React Native?
- → Update React [Native] View on Day Change
- → Shopify iOS SDK - issue converting BuyProductVariant to BuyProduct
- → BigCommerce and shopify API
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → React Native - Differences between Android and IOS
- → What is the difference between React Native and React?
Ad