Swift: The Basics
- 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
Swift: The Basics的更多相关文章
- Swift 笔记汇总
Swift 3 笔记 Swift: The Basics :介绍 Swift 一些零碎的点. Swift: Basic Operators :介绍 Swift 的操作符. Functions : 介绍 ...
- Swift中文手册 -- The Basics
原文:Swift中文手册 -- The Basics 基础部分 Swift 是 iOS 和 OS X 应用开发的一门新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 S ...
- Swift -- 中文版两大官方文档汇总
Swift官方文档由CocoaChina翻译小组精心翻译制作而成,目前两本文档中文版已全部完成!在此,我们对所有参与的译者.组织人员以及工作人员表示衷心的感谢!本文为您提供两本文档的在线阅读以及下载! ...
- An Introduction to Protocol Oriented Programming in Swift
swift面向协议编程的根本原因在于值类型的存在:面向对象必须要有引用类型的支持: Protocol Oriented approach was introduced to resolve some ...
- Swift: Associated Types--为什么协议使用关联类型而不是泛型
关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...
- 随手记Swift基础和Optional Type(问号?和感叹号!)
距离Apple推出Swift已经有几天了,网上也时不时出现"急招Swift程序猿,要求有一天工作经验"的帖子. 看到Swift,除了苹果放的另外一门语言的链接(http://swi ...
- swift的可选值(optional)
苹果那文档写了一大堆也没有好好的写一下可选值(optional)这个东西.就是在有一个“Optional Chaining”的章节,但是也不是很充分的说明.最后找了半天在“the basics”里墨迹 ...
- 【读书笔记】The Swift Programming Language (Swift 4.0.3)
素材:Language Guide 初次接触 Swift,建议先看下 A Swift Tour,否则思维转换会很费力,容易卡死或钻牛角尖. 同样是每一章只总结3个自己认为最重要的点.这样挺好!强迫你去 ...
- Basics
[Basics] 1.You can declare multiple constants or multiple variables on a single line, separated by c ...
随机推荐
- input file文件上传样式
<style> .file-group { position: relative; width: 200px; height: 80px; ...
- 微信JS-SDK实际分享功能
为了净化网络,整顿诱导分享及诱导关注行为,微信于2014年12月30日发布了<微信公众平台关于整顿诱导分享及诱导关注行为的公告>,微信平台开发者发现,原有的微信分享功能不能用了,在ipho ...
- IOS各类问题
1.The resource could not be loaded because the App Transport Security policy requires the use of a s ...
- 关于pcre正则表达式库libpcre
gcc 4.8中已经包含了std regex的头文件 可是没有实现,所以链接是失败的 gcc 4.9完整的支持了c++ 11的regex. 在4.9以前,可以寻求boost的regex. 不过,我更熟 ...
- [Fw]人和人之间在八小时之外的差别
原文地址:http://hankjin.blog.163.com/blog/static/3373193720083249387801/ 业余八小时人的活动千姿百态.八小时以外你在干什么,恰恰决定着你 ...
- net programming guid
Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...
- js 实现 di
前些时候有使用过AngularJS一些时间,最大的感受就是Angular完全颠覆了我们开发Web应用的方式,自己被其许多耳目一新的设计思想所折服. 首先想说的就是依赖注入(DI),这也意味着,你在使用 ...
- Unity3d 读取网络xml
Unity3d 读取网络xml Unity3d 读取网络xml,这个xml文件需要不包含BOM信息,可以用UltraEdit打开xml文件,并且另存为的时候,选择不包含BOM的utf-8格式存储!
- Cylinder
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2374 思路:三分枚举. #include &l ...
- struts2令牌,防止重复提交
struts2的令牌,可以用来防止重复提交,其原理是在提交jsp页面中,写入一个隐藏域name="token",然后在action中定义一个变量token并get.set.在服务器 ...