NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Specialized Extensions

Forums > SwiftUI

Hi! Was doing a lesson of the 100 days of SwiftUI and there was a moment where Paul added an extension to the ShapeStyle protocol instead of the Color one, like this:

extension ShapeStyle where Self == Color { ... }

But even after he kind of explained the reason, i still don't understand why is this a better approach compared to do the extension directly in "Color".

   

This ShapeStyle protocol is what the background() modifier uses, so what we really want to do is extend Color but do so in a way that all the SwiftUI modifiers using ShapeStyle work too.

So other modififier like .foregroundStyle(_:), .stroke(_:style:) and .tint(_:), however you would not be able to use the extension static var darkBackground in these if you did not use ShapeStyle

   

@Renato brings up memories from a year ago!

But even after he kind of explained the reason, i still don't understand why is this a better approach

See -> ShapeStyle Discussion

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.