TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 23 - Custom Modifiers

Forums > 100 Days of SwiftUI

Hello,

I'm trying to reason my way through how this code works for Custom Modifiers.

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello World")
            .titleStyle()
        }
}

struct Title: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.largeTitle)
            .foregroundColor(.white)
            .padding()
            .background(Color.blue)
            .clipShape(RoundedRectangle(cornerRadius: 10))
    }
}

extension View {
    func titleStyle() -> some View {
        modifier(Title())
    }
}

So, from what I understand so far - these are things I can state definitely.

  1. The computed property body contains a text view, with out customer modifer .titleStyle()
  2. titleStyle is an extension to the View type, and we added the func titleStyle() to it. This is why we can just just .titleStyle() as a modifier to any view, rather than have to specify .modifier(Title())
  3. the func titleStyle() takes no parameters and returns some View. it calls modifier(Title())
  4. Title is a struct. it has one function, body. body is what returns our 'some View'.
  5. func body acts on the parameter Content
  6. func body(content: Content) -> some View must be what is required to make the function adhere to the ViewModifer Protocol

What I am hazy on: func body takes a Content parameter - but modifier(Title()) passes no parameter. What is this magic?

I think the clue to that might be in the source code for modifer() - and may be related to a closure in there somewhere?

If anyone can explain more thoroughly, I'd appreciate it! Thank you!

3      

modifier(_:) wraps the View it is attached to in a new type ModifiedContent<Content, Modifier>.

The ModifiedContent type then likely has a body property or function that takes your Content and passes it as the content parameter to your ViewModifier's body function.

So, something like this:

struct ModifiedContent<Content, Modifier> {
    var content: Content
    var modifier: Modifier

    var body: some View { //not really sure what the exact return type would be here
        modifier.body(content: content)
    }
}

It almost certainly gets a lot hairier in there with all the generics and opaque return types involved, but that's probably the gist of it.

4      

Thanks, Roosterboy. This is good practice in accepting I can't know everything, and just moving on until it become clearer!

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.