From 55dc8dde9514bfdbe1626cbec8ef200d791ae9f7 Mon Sep 17 00:00:00 2001 From: H3r0Complex <9460076+H3r0Complex@users.noreply.github.com> Date: Mon, 24 Jun 2019 08:35:17 -0700 Subject: [PATCH] added basic info about type safety and optionals (#27860) * added basic info about type safety and optionals * add link for optionals, renamed links and title also renamed the directory folder * added basic info about type safety and optionals added optionals link and renamed links and directory * added basic info about type safety and optionals added optionals link and renamed links and directory * added basic info about type safety and optionals also added "swift optionals" link and renamed original link to "type safety and type inference" and also renamed directory * added basic info about optionals added link to swift optionals and change the name of the first link and the directory * added info on swift optionals renamed links and directory folder * added info about type safety and optionals added a link to swift optionals and change folder name and a link's name to type safety and inference * added info about optionals added a link to optionals docs, change link and folder name to "type safety and inference", similarly with the title tag * Update index.md * Update index.md * added info on swift optionals and type safety altered link and directory names and added a link to optionals docs * added info for swift type-safety and optionals changed link and directory name to type safe and inference added link to swift optionals doc * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update guide/english/swift/type-safety-and-inference/index.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * changed the directory name to match the title * modified folder name to have the proper format :-) --- guide/english/swift/type-inference/index.md | 25 ------ .../index.md | 89 +++++++++++++++++++ 2 files changed, 89 insertions(+), 25 deletions(-) delete mode 100644 guide/english/swift/type-inference/index.md create mode 100644 guide/english/swift/type-safety-inference-and-optionals/index.md diff --git a/guide/english/swift/type-inference/index.md b/guide/english/swift/type-inference/index.md deleted file mode 100644 index 6a926304dc..0000000000 --- a/guide/english/swift/type-inference/index.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -title: Type Inference ---- -## Type Inference - -Swift uses type inference. So if you write some code like the code in the example below it’s pretty obvious what each type is. - -#### Example: -```swift - let iPhone: String = “iPhone” - let yearIntroduced: Int = 2007 - let isAwesome: Bool = true -``` - -So we can clean up the code to look like the example below, and Swift can infer the type for use. - -#### Example: -```swift - let iPhone = “iPhone” // Inferred as String - let yearIntroduced = 2007 // Inferred as Int - let isAwesome = true // Inferred as Bool -``` - -#### More Information: -- The Swift Programming Language diff --git a/guide/english/swift/type-safety-inference-and-optionals/index.md b/guide/english/swift/type-safety-inference-and-optionals/index.md new file mode 100644 index 0000000000..e897a785f6 --- /dev/null +++ b/guide/english/swift/type-safety-inference-and-optionals/index.md @@ -0,0 +1,89 @@ +--- +title: Type Safety, Inference, and Optionals +--- +## Type Inference + +Swift uses type inference. So if you write some code like the code in the example below it’s pretty obvious what each type is. + +#### Example: + +```swift + let iPhone: String = “iPhone” + let yearIntroduced: Int = 2007 + let isAwesome: Bool = true +``` + +So we can clean up the code to look like the example below, and Swift can infer the type for use. + +#### Example: + +```swift + let iPhone = “iPhone” // Inferred as String + let yearIntroduced = 2007 // Inferred as Int + let isAwesome = true // Inferred as Bool +``` + +## Type Safety + +Swift is a "type-safe" language. This means that by design the language helps programmers be more clear about the types they are using and helps mitigate and/or prevent common type errors - like trying to pass a string where it requires an integer. One core mechanism of this type-safe design are Optional Types. + +## Optional Types + +When a variable's type is an optional type it denotes that its value can have a value **_or_** no value at all (nil). + +You declare an optional by appending a question mark at the end of the type declaration. + +```swift + var optionalFloat: Float? // This is an Optional Float + var optionalString: String? = "an optional string" // and an Optional String + + let normalFloat: Float = 0.5 // This is an ordinary Float + var normalString = "a string" // and a String +``` + +**Note:** since optionalFloat is not assigned a value it is given a default value of nil + +### Unwrapping Optionals + +Optionals are considered a different type then its non-optional variant. For example in the above code "optionalFloat" and "normalFloat" are _not_ the same type. When you try to access an optional variable like you would a normal variable the value would be of Optional(_SomeValue_). + +```swift + var optionalInt: Int? = 5 + let normalInt = 10 + print(optionalInt) // Optional(5) +``` + +If you try to utilize the optional variable like normal you will get an error. +```swift + let sumError = normalInt + optionalInt //error! +``` + +Instead you must first "unwrap" the optional before you can utilize the data. There are a couple of ways doing so: forced unwrapping it and optional binding. + +#### Forced Unwrapping + +If you are **certain** that an optional does contain a value, you can force-unwrap it by appending an exlamation mark at the end of it. + +```swift + if(optionalInt != nil) { // make sure the optionalInt has a value, otherwise will get a runtime error + let sum = normalInt + optionalInt! // <-- forced unwrapped + } +``` + +#### Optional Binding + +Optional Binding is a special kind of condition statement (either with an if or a while) at which it automatically unwraps an optional if it has a value, and then stores that value in the designated variable. That variable can then be utilized within the if/while statements body normally. + +```swift + if let unwrappedInt = optionalInt { + let sum = normalInt + unwrappedInt; // 15 + } + ``` + +If the optional does not have a value (nil), then the condition would be considered false and its code would not execute and the program will continue on normally. + + +#### More Information: +- [Type-Safety and Type Inference](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID322) + +- [Swift Optionals](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID330)