UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 33. Animation

Forums > 100 Days of SwiftUI

  1. onChanged initially suggests - .onchanged({ }) but there're only {} curly braces in tutorial. If there's a difference? It's called trailing closure? Why it's not this .onchanged( ) { }

  2. Why there's no _ in in .onChanged{} modifier

  3. General question about self. As I understood it means that it belongs to struct of the View. Writing wo self also works. What's the difference if I write w/ wo self. ?

DragGesture()
                .onChanged { self.dragAmount = $0.translation }
                .onEnded { _ in
                    self.dragAmount = .zero
                    self.enabled.toggle()
                }

The same with withAnimation . If it's method, why there's no parenthesis.

withAnimation {
                    self.isShowingRed.toggle()
                }
  1. {Optional} There're lots of different modififers for everything, curious how I can discover them on my own? Tutorials are great, but I still forget and it's overal useful to know how to do research orginally?(after I finish #100D...). How those, who write articles/ tutorials/ answers discover them?
  • What I've found - there's a menu for modifiers. It can be found in couple of places. Search it. One way to do is:

    ⇧⌘L - Opens menu, including modifiers.

Also relevant for all the code stuff I suppose.

3      

Good Questions all around. I'll give it my best:

1- As you know, functions (or methods) can sometimes take parameters. These parameters are sometimes closures. Let's say for example, that a function takes 1 parameter with a label "age" and a second parameter which is a closure. Then when you call that function, you would do something like this:

someFunction(age: 43) {  
    // code here
}

But, if the function only takes a closure for it's parameter then you can write it in 2 ways

closureOnlyFunction({
    // code here
})
// OR
closureOnlyFunction {
    // code here
}

Because the closure is the only parameter you can skip the parentheses. By default, the autocomplete will use the first option, but you can use the second version.

2- Some functions provide you with "data" or "results". basically some kind of information. These values are accessible to you within the closure. Sometimes it's just one. Sometimes it's more. In the case of .onEnded we get the final value of the action. We use _ to basically say "we don't care about that value, we won't be using it". Otherwise, we would give that parameter a name and use it inside our closure. (more later on how to know what you get to work with)

3- self took me some time to understand, and I still don't think I understand it 100%. Basically, when we type self, we are referencing self. There is some performance related stuff here. What I can tell you confidently is that recently there has been a change that allows you to skip using self.

This is called implicit self and was requested by the community. So for instance, inside a Button action, we no longer need to use self. However, if you don't use self where it is required, Xcode will let you know. It won't build. My approach with SwiftUI is to not use self unless explicitly required. Once more advanced, and testing shows a need to reference self, I will refactor...

If you want more details about self, check out Sean Allen's video linked below:

https://www.youtube.com/watch?v=bBXdXmUobMw

4- That is the ultimate skill. I would practice this as much as possible. To answer your question though, there are a couple of things to do here.

a- Command + Click on the item you wish to modify and click "jump to definition". This will take you to the source code and the protocol declarations. Inside there, you will find all the details you want. IMPORTANT: this can be daunting. It's not necessarily the easiest thing to read for those of us still early on our journey.

b- Check Developer Documnetation. Whether you do it on the website or Shift + Command + 0 (that's zero) and search there for what you want. Again, it's not exactly the easiest to navigate, but that's where the information is.

c- There is a paid alternative for SwiftUI. If you want a better resource for documentation, with examples, the app is called "A Companion to SwiftUI". Personally, I absolutely love it. But again, it's paid so not for everyone.

To use an example from what we discussed above, try it on .onEnded. If you open the Developer Documentation from Xcode (or the site) and search for onEnded you see some details. This is the declaration:

func onEnded(_ action: @escaping (Angle) -> Void) -> _EndedGesture<RotationGesture>

That tells you quite a bit already. It takes a single parameter (unnamed at the call site) and returns _EndedGesture<RotationGesture> Not very helpful maybe, but if you read the parameter section:

The action to perform when this gesture ends. The action closure’s parameter contains the final value of the gesture.

It says that the closure will contain the final value. So now you know you have to use _ in if you don't care or someName in if you do.

There is another way these individuals get their information. They checkout Swift.org and read the source code. But that's a bit advanced for me and I'm not really interested in it.

I hope the above answers your questions 😉

4      

@MarcusKay thanks immense for profound answer!!! As of question 4, thank you very much for tips!

Also, understood why

.onChanged { self.dragAmount = $0.translation }

doesn't need _ in - because it has $0 inside.

I found helpful to read swift.org along with tutorials (I'm only on the 1/3 of it). It's although not easy read, but if distributed well through span of time, it's, probably, possible to finish. I think it gives the foundational knowledge, that developer can leverage further, and more smoothly adapt to changes whatever they be.

That also helped me in a sense - when I do tutorials, I'm more aware what the tutor is talking about and less loose the thread of thought.

3      

Hacking with Swift is sponsored by RevenueCat

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

Learn more here

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.