Nineth Day of Swift learning: Optional types and application scenarios

Source: Internet
Author: User

An introduction to the optional types
    • Attention:
      • A knowledge point that is more understood in Swift when an optional type is available
      • Learn more about using Xcode tips to get started
      • Slowly understand the principles and benefits as you learn.
    • Concept:
      • In OC Development, if a variable is paused, it can be assigned a value of 0 (base attribute type) or an assignment of Null (object type)
      • Nil is also a special type in swift development. It is not possible to assign a value because it does not match the true type (Swift is a strongly typed language)
      • However, in the development of the value of nil, it is inevitable. Therefore, optional types are available
    • Values for the optional type:
      • Null value
      • has a value
Define an optional type
    • There are two ways to define an optional type
      • The most basic wording
      • Grammar sugar (common)
// 错误写法// let string : String = nil// 正确写法:// 注意:name的类型是一个可选类型,但是该可选类型中可以存放字符串.// 写法一:定义可选类型let name : Optional<String> = nil// 写法二:定义可选类型,语法糖(常用)let name : String? = nil
Use of optional types
Walkthrough One: Assigning a value to an optional typeDefine an optional typevar string:optional<String> =NilAssigning a value to an optional typeError notation: Therefore, the optional type can only hold strings string =123Correct wording: string ="Hello World"Print resultsprint (string) //Result: Optional ("Hello World") \n//because the print out is optional type, all will take Optional//Walkthrough Two: Remove the value of the optional type / /Remove the actual value of the optional type (unpacking) print (string!) //Result: Hello world\n//Note: If the optional type is nil, forcing the fetch of the value (unpacking), the error string = nilprint (string!) //error //correct wording: if string! = nil {print (string!)} //simple notation: To make it easy to use String//optional bindings in an if statement if let str = string {print (str)}< /span>                 
Import UIKit//all properties in the class must have an initialization value when the object is initializedclassPerson:nsobject {var name:string?//has a value or is a null valuevar view:uiview?}//1. Define an optional type//1> How to define optional types in general//var name:optional<string>//2> Grammar Sugarvar name:string?//2. Assigning a value to an optional typeName =" Why"//3. Take a value from an optional type//Optional ("why")print (name)//to take a value from an optional type: Optional type!--> forced unpacking//print (name!)//4. Note: If there is no value in the optional type, forcing the unpacking program crashes//forcing unpacking is a very risky operation: it is recommended to determine if there are any values in the optional type before unpacking.ifName! =Nil {print (name!) Print (name!) Print (name!)}//5. Optional Bindings//1> Determines if the name has a value and does not execute {} If there is no value.//2> If there is a value, the optional type is unpacked and the value after the unpacking is assigned to the preceding constant//if let Tempname = name {//print (tempname)//}ifLet name =Name { print (name) print (name) print}

Two: Optional type of application scenario

Import UIKit/*1: When the type of two objects is not the same, can not be directly assigned, Nsurl (string:urlstr) Create a URL to get the type is Nsurl? Optional type, can also not label Nsurl?, with type Deduction 2: Optional type of unpacking: 1:! To be forced to unpack, you must first determine whether it is nil, otherwise the forced unpacking of nil will occur when the crash 2: You can also use optional binding*/Let urlstr="www.baidu.com"Let url:nsurl? = Nsurl (string: URLSTR)//if URL! = Nil {//Let request = Nsurlrequest (url:url! as URL)//}//Optional BindingsifLet URL =URL {Let request= Nsurlrequest (Url:url asURL)} 

Nineth Day of Swift learning: Optional types and application scenarios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.