nicklockwood,
@nicklockwood@mastodon.social avatar

In a fit of madness I've agreed to give an internal talk about advanced Swift at my company. Has anybody got any favorite Swift tricks they'd like to share?

ctietze,
@ctietze@mastodon.social avatar
matadan,
@matadan@mastodon.social avatar

@nicklockwood have you got that you can use ‘case var’ as well as ‘case let’ in switch statements?

ctietze, (edited )
@ctietze@mastodon.social avatar

@nicklockwood where do you draw the line?

Knowing that for element in array works, but not for let element in array, but of course for var element in array does, is that advanced? :D

nicklockwood,
@nicklockwood@mastodon.social avatar

@ctietze this is a great tip - also other related for loop syntax quirks like for case let foo as type and where clauses

cocoafrog,
@cocoafrog@hachyderm.io avatar

@ctietze @nicklockwood also loops can have where clauses, eg

for batch in batches where !batch.isEmpty { }

And let for optional unwrap works in loops as well:

var next: Node?
while let current = next {
// do stuff
next = current.parent
}

And you can combine the two.

helge,
@helge@mastodon.social avatar

@cocoafrog @ctietze @nicklockwood For this you can also use sequence, like for item in sequence(item) { $0.parent } { ... }.
But I also like the where, though I don't actually use it that often.

cocoafrog,
@cocoafrog@hachyderm.io avatar

@helge @ctietze @nicklockwood oh yeah, sequence(next:) is one I learned recently and is great! It was more about the general concept of for let.

ctietze,
@ctietze@mastodon.social avatar

@cocoafrog @helge @nicklockwood What, where does that come from :O

I could've used that in tree walking recently. I'm giving back my "advanced Swift" badge.

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze @cocoafrog @helge @nicklockwood 🤦🏽‍♂️ feels like I got to re-review the standard lib one of these days

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ethstaker
  • DreamBathrooms
  • mdbf
  • InstantRegret
  • ngwrru68w68
  • magazineikmin
  • everett
  • thenastyranch
  • Youngstown
  • slotface
  • cisconetworking
  • kavyap
  • osvaldo12
  • modclub
  • megavids
  • GTA5RPClips
  • khanakhh
  • tacticalgear
  • Durango
  • rosin
  • normalnudes
  • Leos
  • provamag3
  • tester
  • cubers
  • anitta
  • JUstTest
  • lostlight
  • All magazines