No idea

I have a structure TodoItem

struct TodoItem: Codable, Hashable, Identifiable {

    var id: UUID = UUID()

    var item: String

    var done: Bool = false

}

And I have an array of them, and I’d like to find out a way to calculate(in a array) how many are done; that is, myArray.count / itemsDone

.count is easy, itemsDone can be achieved like this:

extension Array where Element == TodoItem {

    var itemsDone: Double {

        var done: Double = 0

        for i in self {

            if i.done { done += 1 }

        }

        return done

    }

}

but it didn’t.

struct ContentView: View {
    @State var todos: [TodoItem]
…
    ProgressView(value: todos.itemsDone / todos.count) // Error

My Playground won’t let me copy the error(no idea why) and my family’s iPad is in Chinese so one is Chinese. I’ll try it on Xcode and see if I can find out more. will update with the error.

EDiT nor will this work:

ProgressView(value: todos.itemsDone, total: todos.count)
  • Could you write the error you get ? Do you get 0 ? Do you get a crash ?

  • error on binding and numbers I’ll outside so I’m on playgrounds and it’s in Chinese…

  • So I’m not directly pasting the error. It’s sth like not able to convert “dynamic” sth and Binding<_> and lots else

Add a Comment

Replies

I guess, those values should be converted as Float or Double.

ProgressView(value: Double(todos.itemsDone) / Double(todos.count))

does not work?

Add a Comment

OK I figured it out my self