Swift: The Basics
2024-10-14 16:05:24
- Swift是类型安全的语言;
- Swift introduces optional types, which handle the absence of a value. Optional say either "there is a value, and it equals x" or "there isn't a value at all".
- 类型注解
var welcomeMessage: String
var red, green, double: Double - 类型别名(Type Alias)
typealias AudioSample = UInt16 var maxAmplitudeFound = AudioSample.min
- 元组(Tuples):元组内的值可以是不同类型的任何值。
let http404Error = (, "Not Found")
//http404Error的类型是(Int, String)分解(decompose)元组:可以使用下划线来忽略一些值
let (statusCode, _) = http404Error
print("The status code is \(status)")也可以使用下标来取出单个值:
print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found"可以在定义元组的时候,给每个元素命名:
let http200Status = (statusCode: , description: "OK")
这个时候,可以这样获取每个元素的值:
print("The status code is \(http200Status.statusCode)")
- Optional type
let possibleNumber = ""
let convertedNumber = Int(possibleNumber)
//convertedNumber的类型是 Int?, 不是IntSwift的nil跟oc中的nil不同。In Objective-C, nil is a pointer to a nonexistent object. In Swift, nil is not a pointer -- it's the absence of a value of certain type. Optionals of any type can be set to nil, not just object types.
forced unwrapping
optional binding
implicity unwrapped optionals
最新文章
- php libevent 扩展使用示例
- JAVA程序操作hbase的Maven配置pom.xml文件
- SharpDevelop学习笔记(5)—— AddIns系统详解
- JSON学习之二
- 获取dom元素的宽度和高度
- navicat内的主键和外键
- Creating Help Pages for ASP.NET Web API -摘自网络
- Contiki学习入门之概览
- TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间
- 如何让旧版IE浏览器认识HTML5元素
- iOS截取http/https流量
- Vue.js父与子组件之间传参
- 游戏行业DDoS攻击解决方案
- mysql悲观锁
- Hadoop生态圈-离线方式部署Cloudera Manager5.15.1
- learning scala read from file
- python打包--pyinstaller打包报错
- 在Asp.net core中使用WebScocket
- iOS presentedViewController和presentingViewController的区别
- Delete触发器