Last active
August 23, 2019 22:12
-
-
Save PaulWoodIII/b09c2e084e3c94fd3339ac7b4153cadd to your computer and use it in GitHub Desktop.
playing around with published sequence types, you need wrapper objects
This file contains hidden or 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
| // | |
| // ArrayOfArrayView.swift | |
| // PublishedList | |
| // | |
| // Created by Paul Wood on 8/23/19. | |
| // Copyright © 2019 Paul Wood. All rights reserved. | |
| // | |
| import SwiftUI | |
| import Combine | |
| struct IntItem: Identifiable, ExpressibleByIntegerLiteral { | |
| var value: Int | |
| var id: Int { return value } | |
| init(integerLiteral value: Int) { | |
| self.value = value | |
| } | |
| init(_ value: Int) { | |
| self.value = value | |
| } | |
| } | |
| struct ArrayHolder<U: Identifiable>: ExpressibleByArrayLiteral, Identifiable { | |
| typealias ArrayLiteralElement = U | |
| var array: [U] | |
| var id: UUID = UUID() | |
| init(arrayLiteral elements: U...) { | |
| self.array = elements | |
| } | |
| init(_ array: [U]) { | |
| self.array = array | |
| } | |
| } | |
| class DataSource: ObservableObject { | |
| @Published var arrayHolder: [ArrayHolder<IntItem>] = [[0],[0,1]] | |
| func plus() { | |
| if arrayHolder.count < 5 { | |
| if arrayHolder.count > 0 { | |
| let currentArray = arrayHolder[arrayHolder.count - 1] | |
| let nextToAdd = IntItem(currentArray.array.last!.value + 1) | |
| let nextArray = currentArray.array + [nextToAdd] | |
| let nextHolder = ArrayHolder(nextArray) | |
| arrayHolder.append(nextHolder) | |
| } else { | |
| arrayHolder.append(ArrayHolder<IntItem>([0])) | |
| } | |
| } | |
| } | |
| func minus() { | |
| if arrayHolder.count > 0 { | |
| arrayHolder.remove(at: arrayHolder.count - 1) | |
| } | |
| } | |
| } | |
| struct ArrayOfArrayView: View { | |
| @ObservedObject var ds: DataSource = DataSource() | |
| var body: some View { | |
| VStack{ | |
| Spacer() | |
| HStack(alignment: .firstTextBaseline) { | |
| Text("List of Items").font(.title) | |
| Text("Maximum of 5").font(.subheadline) | |
| } | |
| VStack{ | |
| ForEach(ds.arrayHolder){ (item: ArrayHolder) in | |
| HStack { | |
| ForEach(item.array) { (i: IntItem) in | |
| Text("\(i.value)").font(.headline) | |
| } | |
| } | |
| } | |
| } | |
| HStack{ | |
| Spacer() | |
| Button(action: ds.plus) { | |
| Image(systemName: "plus") | |
| .font(.title) | |
| .padding() | |
| .background( | |
| Circle().foregroundColor(Color.accentColor.opacity(0.3)) | |
| ) | |
| } | |
| Spacer() | |
| Button(action: ds.minus) { | |
| Image(systemName: "minus") | |
| .font(.title) | |
| .padding() | |
| .background( | |
| Circle().foregroundColor(Color.accentColor.opacity(0.3)) | |
| ) | |
| } | |
| Spacer() | |
| } | |
| Spacer() | |
| }.animation(.default) | |
| } | |
| } | |
| struct ContentView_Previews: PreviewProvider { | |
| static var previews: some View { | |
| ArrayOfArrayView() | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
something like [[Int]] where int is Identifiable via extension doesn't create a good sample code.
In short you should prefer to use structs/classes to abstract the data that is displayed to a user even in a tutorial. You can use
ExpressibleByIntegerLiteralorExpressibleByArrayLiteralto shorten your code to something newcomers to the language can understand. This also prevents issues with operator overloading for example Array.append doesn't always do what you expect.