Skip to content

Instantly share code, notes, and snippets.

@below
Last active August 27, 2020 10:33
Show Gist options
  • Save below/354f9c602aa9df0decc164f118800240 to your computer and use it in GitHub Desktop.
Save below/354f9c602aa9df0decc164f118800240 to your computer and use it in GitHub Desktop.
Why is this assignment not working … it compiles
//: A UIKit based Playground for presenting user interface
//
// QuizList.swift
// QuizList
//
// Created by Alexander v. Below on 09.06.19.
// Copyright © 2019 None. All rights reserved.
//
import SwiftUI
import PlaygroundSupport
struct QuizListElement: Equatable, Codable {
var number: Int
var text: String
}
struct QuizList : Collection, Codable, Equatable {
typealias Index = Int
typealias Element = QuizListElement
init() {
itemList = [
Element(number: 1, text: "Surprise"),
Element(number: 2, text: "Fear"),
Element(number: 3, text: "Ruthless efficiency"),
Element(number: 4, text: "Almost fanatical devotion to the Pope"),
Element(number: 5, text: "Nice red uniforms"),
]
}
private let itemList: [Element]
var startIndex: Int { return itemList.startIndex }
var endIndex: Int { return itemList.endIndex }
subscript(position: Int) -> Element {
return itemList[position]
}
func index(after i: Int) -> Int {
return itemList.index(after: i)
}
static func ==(lhs: [Element], rhs: QuizList) -> Bool {
return lhs == rhs.itemList
}
}
struct ListView: View {
var list: QuizList
@State var quizList = [QuizListElement]()
internal func reshuffle () {
var mutableList = [QuizListElement]()
for element in self.list {
mutableList.append(element)
}
let shuffledList = mutableList.shuffled()
debugPrint("shuffledList has \(shuffledList.count) elements")
self.quizList = shuffledList
debugPrint("quizList has \(quizList.count) elements")
}
init(list: QuizList) {
self.list = list
reshuffle()
}
var body: some View {
List {
ForEach (0..<quizList.count) { i in
Text(quizList[i].text)
}
.onMove(perform: move)
}
}
func move(from indices: IndexSet, to newOffset: Int) {
quizList.move(fromOffsets: indices, toOffset: newOffset)
}
}
PlaygroundPage.current.setLiveView(ListView(list: QuizList()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment