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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.