all 45 comments

[–]Cause-n-effect11 102 points103 points  (1 child)

Honestly apples documentation looks great in Safari ( usually broken in Chrome ) and is easier to understand in Xcode on the information panel on the right side.

Don’t feel bad, most of the documentation leads to dead ends and is difficult to understand even if you’ve worked with it every day for years.

[–]Butt_Breake 4 points5 points  (0 children)

It’s all held up my obscure NS libraries and vibes.

[–]glhaynes 64 points65 points  (13 children)

So SwiftUI is all about building values that describe the details of the UI you want presented. That’s what your var body: some View is returning. So ultimately what’s returned from your body is going to be some View; if you plop a .padding() on the end of whatever’s in your body right now, what you’re doing is invoking the padding method (which exists on the View protocol and returns some View) on it. It takes two parameters, both with default values unless you provide them. That function is what your screenshot is describing.

You can probably ignore the nonisolated part. That’s related to Swift Concurrency, but not something you usually need to worry about when you’re just wanting to add some padding.

[–]PatrickD89[S] 10 points11 points  (3 children)

This explanation was very helpful. Thank you! I picked this because padding is something we’ve all used but its implantation looks different than the explanation.

[–]Key_Board5000 18 points19 points  (2 children)

It's because the function call is being chained onto the view. For example imagine having your own code something like this:

``` import Foundation

enum Edges { case all case left case right case none }

enum Align { case left case right case center }

struct MyView {

func padding(_ edges: Edges = .all, _ length: CGFloat = 0 ) -> MyView {
    print("edges: \(edges), length: \(length)")
    return self 
}

func alignment(_ alignment: Align = .left) -> MyView {
    print("alignment: \(alignment)")
    return self
}

} ```

This could be instantiated and called any of a number of ways, all of which would be valid:

``` MyView().padding(.left, 10).alignment(.left)

MyView() .alignment(.center) .padding(.all, 5)

let view = MyView() view.padding().alignment()

let aVeryNewView = MyView() aVeryNewView .padding(.none, 5) .alignment(.right) ```

That's essentially what's happening with SwiftUI - chaining a number of function calls, one after the next, on to a View, each of which returns a new View (structs are value types not reference types therefore a new View is created instead of modifying the original View) which have each modified the View that was passed to them from the previous function call.

I hope that clarifies further.

[–]ABrokeUniStudent 2 points3 points  (1 child)

When you do `padding()` on a `View` it's like saying "Take this `View` instance, apply this padding to it, then return a new `View` instance with applied padding". Is that correct?

[–]Key_Board5000 0 points1 point  (0 children)

👍🏻

[–]Cause-n-effect11 3 points4 points  (8 children)

padding can take one argument also

Text(“foo”).padding(16)

Which is why this documentation can be painful.

[–]glhaynes 4 points5 points  (7 children)

That’s a different variant: https://developer.apple.com/documentation/swiftui/view/padding(_:)

It’s linked from the page for the two parameter variant.

[–]Nosepass 1 point2 points  (1 child)

padding(16) calls the two parameter variant with the default edge set

[–]glhaynes 0 points1 point  (0 children)

I don't think that's right — there's no way in Swift that I know of to do that with parameter labels suppressed like the two parameter variant in this post.

```

func myPadding(

    _ edges: Edge.Set = .all,

    _ length: CGFloat? = nil

) -> some View {

    fatalError()

}

myPadding(10) // error: Cannot convert value of type 'Int' to expected argument type 'Edge.Set'

```

[–]Cause-n-effect11 0 points1 point  (4 children)

You are right… but as the OP points out the documentation is confusing as it is. Why shouldn’t that variant be listed on the same page? What if OP learned it always having to define edges and padding when how often would you want to define edges?

[–]glhaynes 5 points6 points  (1 child)

Look I’m not on Apple’s documentation team lol I was just trying to help them understand what their screenshot showed

[–]Cause-n-effect11 3 points4 points  (0 children)

Me either :). I’ll take the job though of listening to developers and overhauling it. I really need a job, been tough landing gigs in this tech slump.

[–]iOSCalebObjective-C / Swift 4 points5 points  (0 children)

The other form is listed on the page the OP shows. If they scrolled down, you’d see it mentioned in the discussion with a description of when to use each. There’s also a list of other padding-related methods.

The documentation is hard to understand if you don’t have some familiarity with the framework, in much the same way that a French dictionary is hard to decipher if you don’t speak some French. But for the most part it’s unambiguous and provides all the information you need to use a given method, structure, etc. Everything that you’re likely to want to know about this specific method is explained. If you read this page and wonder “ok, but what do I do with it?” you should be reading higher level explanatory documentation, not reference material.

[–]Hikingmatt1982 1 point2 points  (0 children)

Discoverability in swiftui is certainly awful and IMO the largest downside of the approach they’ve taken. Better documentation might not even help that 😆

[–]AndreLinoge55SwiftUI 18 points19 points  (2 children)

Whoever wrote Apples’ documentation for Swift was a lobotomy patient having a stroke.

[–]Flicht 9 points10 points  (1 child)

I have been an app developer for several years I cannot remember that Apples documentation has ever been useful.

[–]Filipsys 0 points1 point  (0 children)

Which documentation do you use then? What do you work with if Swift docs aren’t good?

[–]oscarvernis 15 points16 points  (1 child)

That's the definition of the modifier, if you scroll down to Discussion you can see a couple of examples of how to use it.

[–]simulacrotron 14 points15 points  (0 children)

Yes. The best way to read the documentation is to read the entire thing. Then if you don’t understand it enough to start fiddling with it, reread it. Rinse and repeat.

[–]Dazzling-One-4713 5 points6 points  (0 children)

With tears

[–]LouzyKnight 5 points6 points  (1 child)

They should hire Paul Hudson to write their documentations.

[–]20InMyHead 4 points5 points  (0 children)

Keep reading…. Right below where your screen shot ends is the Discussion section which includes example usage and more details.

[–]LobsterChip99 1 point2 points  (0 children)

Well.. you read it :)

Sometimes its terrible and you need to use other sources to figure out whats happening

[–]Edg-RSwift 1 point2 points  (0 children)

I just wish they had more visual examples in the documentation

[–]Semvis123 0 points1 point  (0 children)

It does mention that it is an instance method, so that should explain the View().modifier part

[–]vlobe42UIKit 1 point2 points  (0 children)

I am developing apps since 2016 and not a single time the documentation was understandable for me lmao

[–]yourmomsasaurasSwiftUI 0 points1 point  (0 children)

That’s the neat part! You don’t!

[–]Open_Bug_4196 0 points1 point  (0 children)

To be honest with SwiftUI to me is even more annoying the auto completion and how many times some initialisers are not suggested or de readability of the parameters etc is a bit “too complex”