UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Dictionary access within SwiftUI View

Forums > SwiftUI

Hi all,

I want to access a Dictionary within a View.

The access is previously done via plain array, now I changed it to an dictionary: https://github.com/citoki/workout-activity-rings/commit/a2cc75aa77cb95ed06dec07f29f307a8d5e80805#diff-8cab67a1fadc75825c26beb1733facbcL226-L240

In line 258 you can see the access to the dictionary key:

//.stroke(self.ringColor[3], lineWidth: self.ringThickness) // old
.stroke(self.ringColor[.outline], lineWidth: self.ringThickness) // new

Unfortunately, this doesn't work.

Error message: 'Int' is not convertible to 'Double'

I get a confusing error message on a code line: https://github.com/citoki/workout-activity-rings/blob/a2cc75aa77cb95ed06dec07f29f307a8d5e80805/WorkoutActivityRings/WorkoutActivityRings/CirclesWorkout.swift#L304

3      

I put together a small playground, but the error message I received was Argument type 'Color?' does not conform to expected type 'ShapeStyle', which would make more sense given that self.ringColor[.outline] will return Color?.

Here's my code

import SwiftUI

enum ColorType: String {
    case base
    case light
    case lightCircleEnd
    case outline
}

private var ringColor = [ColorType: Color]()
ringColor[.outline] = Color.red

Circle()
    .stroke(ringColor[.outline], lineWidth: 40.0)

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.