Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic data types of Swift

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what the basic data types of Swift are, which are very detailed and have certain reference value. Friends who are interested must finish reading them.

Swift basic data type

1. Integer var intType: Int = 11print (intType) / / 112. Floating-point var floatType: Float = 3140 / / 10, e to the n power of 10, 3.14 * (10 ^ 3) = 3140.0print (floatType) / / 3140.0 var floatType2: Float = 0x2p3 / / hexadecimal, p to the n power of 2, 2 * (2 ^ 3) = 16.0print (floatType2) / / 16.03. Boolean var boolType = true / / or falseprint (boolType) / / true4. Tuple

Allow unrelated types to be freely combined into new collection types

/ / define tuple, specify parameter name var person: (name: String, age: Int) = ("William", 18) print ("name:", person.name, ", age:", person.age) / / name: William, age: 18amp / tuple does not specify parameter name Only specify type var city: (String, Int) = ("Shanghai", 200000) / / access print (city.0, city.1) / / Shanghai 200000 name / decompose tuple var (name, code) = cityprint (name, code) / / Shanghai 2000005. Optional value Optional

The common type is packaged with Optional to monitor the null value. Similar to the treatment of nullable types in Kotlin, that is, whether to add a question mark after the type or instance.

If you add a question mark after the instance, it is equivalent to a null check for the value of the instance when the instance is called.

If you add a question mark after the type, it is equivalent to specifying that the value of the current variable can be empty nil

/ / single Optional binding, if-let structure var obj: String? = "Wuhan" if let temp = obj {/ / when obj is not nil Assign obj to temp print ("hello", temp) / / hello Wuhan} else {/ / otherwise re-assign obj obj = "hello, if-let" print (obj)} / / bind multiple Optional to var obj1:Int? = 1var obj2:Int? = 2if let temp1 = obj1, let temp2 = obj2, temp1 < temp2 {/ / if all are not nil and satisfy the clause temp1 < temp2 print (temp1) Temp2) / / 12} else {print ("obj1 or obj2 may be nil")} / / Optional Empty, need to unpack var obj4:Int?obj4 = 3max / compiler exception, no unpacking / / print (obj4 + 1) / / the following will automatically unpack var obj5:Int! / / or Intobj5 = 3print (obj5 + 2) / / 5 type alias

Type aliases, flexible use to optimize the readability of the code

/ / specify the alias Weight for the String type, and then you can use Weighttypealias Weight = Stringvar weight:Weight = "50kg" print (weight) / / 50kg to automatically infer and optimize readability as you would with String. 2 var 8-pound 10-pound hexadecimal representation / / automatic inference type var a = 10, b:Int = 20, c = "hello swift" print (a, b, c) / 10 20 hello swift// can be filled with bits before the value, and underscores can be added. Increase readability var number1 = 001.234 / / 1.234var number2 = 1x234 / / 1234 var type_16 / print multiple values separated by commas print (number1, number2) / / 1.234 1234 var type_10 / binary var type_2 = 0b01// octal var type_8 = 0o02// decimal var type_10 = 3 / 0x04print (type_2, type_8, type_10 Type_16) / / 1 2 3 4 and above are all the contents of the article "what are the basic data types of Swift" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report