Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

A Shorter Version of if let

learningcode_x1mckf by learningcode_x1mckf
September 6, 2022
in Swift
0
A Shorter Version of if let
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

Aside from all of the new features of SwiftUI in iOS 16, Apple additionally introduced Swift 5.7 which is able to come together with the discharge of Xcode 14. Let’s try one minor however welcome change in Swift 5.7.

Swift has the idea of optionals that many programming languages don’t have. An non-compulsory sort implies that it may well both have a worth or there isn’t a worth. Swift forces you to examine if an non-compulsory has a worth earlier than utilizing it.

swift-57-optional-binding

Elective Binding is a standard solution to discover out whether or not an non-compulsory has a worth or not. Here’s a pattern code snippet utilizing non-compulsory binding:

var telephone: String?

 

if let myPhone = telephone

    print(“Calling “ + myPhone)

In the event you’re new to Swift, the if let key phrase implies that if the non-compulsory telephone accommodates a worth, the worth is saved to myPhone. Contained in the if block, myPhone is a continuing that should comprise a worth.

To simplify the fixed or variable naming, we normally write the code like this:

if let telephone = telephone

    print(“Calling “ + telephone)

We make the fixed identify the identical because the non-compulsory.

Elective Binding in Swift 5.7

In Swift 5.7, Apple additional permits us to simplify the code like beneath:

if let telephone

    print(“Calling “ + telephone)

It is a minor change in Swift 5.7. Nonetheless, as non-compulsory binding is usually utilized in writing Swift code, this could prevent just a few keystrokes and make the code extra readable.

Observe: If you’re new to Swift, you’ll be able to try our free Swift guide to start out studying the Swift programming language.

Simon Ng


Founding father of AppCoda. Writer of a number of iOS programming books together with Starting iOS Programming with Swift and Mastering SwiftUI. iOS App Developer and Blogger. Comply with me at Fb, Twitter and Google+.

Tutorial

Mastering Swift: Enumerations, Closures, Generics, Protocols and High Order Functions


iOS

Using Swift Protocols to Manage App Configuration


Announcement

Our Swift Programming Book for Beginners Now Supports iOS 9, Xcode 7 and Swift 2






Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

The abstract Vapor service factory design pattern

by learningcode_x1mckf
February 1, 2023
0
The abstract Vapor service factory design pattern

I've written a number of articles about manufacturing unit design patterns on my weblog and this time I might like to speak a couple of particular one, which...

Read more

SwiftNIO tutorial – The echo server

by learningcode_x1mckf
January 27, 2023
0
SwiftNIO tutorial – The echo server

Intoducing SwiftNIO In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops...

Read more

Introducing – Vapor cheatsheet – The.Swift.Dev.

by learningcode_x1mckf
January 23, 2023
0
Introducing – Vapor cheatsheet – The.Swift.Dev.

Out there on Gumroad Thanks for supporting my work by buying the cheatsheet. 🙏 Download now A whole Vapor framework reference for novices. greater than...

Read more

iCloud Programming Tutorial for iOS: An Introduction

by learningcode_x1mckf
January 18, 2023
0
iCloud Programming Tutorial for iOS: An Introduction

Editor’s observe: This week, we work with Ziad Tamim once more to provide you an introduction of iCloud programming. You’ll learn to save and retrieve knowledge from iCloud.On...

Read more

Easy multipart file upload for Swift

by learningcode_x1mckf
January 18, 2023
0
Easy multipart file upload for Swift

I imagine that you've got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not,...

Read more
Next Post
Inspiring Young People to Learn Python With Mission Encodeable – The Real Python Podcast

Inspiring Young People to Learn Python With Mission Encodeable – The Real Python Podcast

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Understanding JavaScript closure and JavaScript scope

Understanding JavaScript closure and JavaScript scope

November 5, 2022
All about the Bool type in Swift

All about the Bool type in Swift

September 14, 2022
A generic CRUD solution for Vapor 4

A generic CRUD solution for Vapor 4

September 23, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?