GO FURTHER, FASTER: Try the Swift Career Accelerator 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".

2      

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

2      

@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

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

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.