NEW: Learn to build the incredible iOS 15 Weather app today! >>

SOLVED: When is it ok to leave parameters out

Forums > 100 Days of SwiftUI

I have been looking at the function definitions that XCode pops up when I type the opening parenthises of a function call - trying to understand the syntax better. I am a bit confused about whether there is a way to know if it's ok to leave the parameters out. Paul seems to leave parameters out fairly often.

An example is: when following the Color and Frames lesson Paul types this @ 3:00

.frame(minWidth: 200, maxWidth: .infinity, maxHeight: 200) 

but when I type the open parenthises XCode shows this

.frame(minWidth: CGFloat?, idealWidth: CGFloat?, maxWidth: GFloat?, minHeight: CGFloat?, idealHeight: CGFloat?, maxHeight: CGFloat?, alignment: Alignment)

I assume that Swift automagically decides optional parameters can default to nil but the last parameter, alignment, seems like it is required - however Paul leaves it out and it still works fine. I guess the developer has specified a default parameter for it - but how do I know that. I have tried searching the documentation using the app that XCode opens when selecting Developer Documentation from the Help menu but I haven't managed to get anything useful out of it - seaching for frame or .frame doesn't seem to provide any useful information.

Does anyone have any hints/tips on how to know then it's ok to leave parameters out?

   

If you cmd-click on frame (I think it's cmd-click by default but I may have changed mine at some point so you might need to experiment), you can see the documentation for it.

func frame(minWidth: CGFloat? = nil, idealWidth: 
    CGFloat? = nil, maxWidth: CGFloat? = nil, 
    minHeight: CGFloat? = nil, idealHeight: CGFloat? = 
    nil, maxHeight: CGFloat? = nil, alignment: 
    Alignment = .center) -> some View

You can see that every one of frame's parameters has a default value and you can leave out the ones you don't need.

BTW, the documentation that comes up in Xcode is the same as what Apple has on their website so you can also look there. For instance: frame

1      

Thanks again @roosterboy. It's option-click for me.
There must be some magic sauce for getting good results when searching the documentation. When I search for frame in the documentation I don't get the page you have linked, I get this list that isn't useful at all - but the option-click has a good amount of detail in it.

   

I never use the search function on Apple's site; too much noise in the results.

I always use a google search of "swiftui _____" with the blank being filled by whatever term I'm looking for. If you use a tool like Alfred or Keyboard Maestro, you can even set up a custom search to do it for you. For instance, in Alfred I set up a custom web search using the keyword ui to create an appropriate google search.

Apple's documentation page is usually pretty high in the results and I often find other pages that can be useful as well.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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.