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


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

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


Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.