swift语言点评二

一、数据类型

1、基础类型的封装

Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Doubleand Float for floating-point values

2、新类型

Swift introduces advanced types not found in Objective-C, such as tuples.

Tuples enable you to create and pass around groupings of values.

3、类型安全语言

Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with.

4、数据类型转化

  1. let three = 3
  2. let pointOneFourOneFiveNine = 0.14159
  3. let pi = Double(three) + pointOneFourOneFiveNine
  4. // pi equals 3.14159, and is inferred to be of type Double

5、Tuples

  1. let http404Error = (404, "Not Found")
  1. let (statusCode, statusMessage) = http404Error
  2. print("The status code is \(statusCode)")
  3. // Prints "The status code is 404"
  4. print("The status message is \(statusMessage)")
  5. // Prints "The status message is Not Found"

6、Optionals

  1. let possibleNumber = "123"
  2. let convertedNumber = Int(possibleNumber)

  类型推断 convertedNumber:Int?

7、Optional Binding

  You use optional binding to find out whether an optional contains a value

8、Implicitly Unwrapped Optionals

. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.

原文地址:https://www.cnblogs.com/feng9exe/p/8677949.html

时间: 2024-07-31 21:08:46

swift语言点评二的相关文章

Swift语言指南(二)--语言基础之注释和分号

注释 通过注释向自己的代码中注入不可执行的文本,作为你自己的笔记或提示.Swift编译器运行时会忽略注释. Swift的注释与C语言极其相似,单行注释以两个反斜线开头: //这是一行注释 多行注释以/*开始,以*/结束: ? 1 2 3 <span style="color: rgb(0, 128, 0);">/* 这也是一条注释, 但跨越多行 */ </span> 与 C 语言的多行注释有所不同的是,Swift 的多行注释可以嵌套在其他多行注释内部.写法是在一

swift语言点评十二-Subscripts

Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence. 下标的形式和函数相同,并且set和get合一 subscript(row: Int, column: Int) -> Double 比较: In addition to simple propert

swift语言点评八-枚举

总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a "raw" value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type. You can defin

swift语言点评十七-Designated Initializers and Convenience Initializers

Swift defines two kinds of initializers for class types to help ensure all stored properties receive an initial value. These are known as designated initializers and convenience initializers. Designated:指定的:特指的 全初始化与部分初始化 Designated Initializers and

swift语言点评三 - Basic Operators

1.Tuples are compared from left to right, one value at a time, until the comparison finds two values that aren't equal. (1, "zebra") < (2, "apple") You can compare two tuples if they have the same type and the same number of values.

苹果新的编程语言 Swift 语言进阶(十二)--选项链

选项链是使用选项来查询和调用其属性.方法或下标的一个过程,如果选项包含一个值,则属性.方法.下标的查询和调用成功,否则,调用返回nil. 选项链能用在任何类型的选项来检查对其一个属性.方法.下标的查询和调用是否成功. 选项链可以作为强制展开的替代方式使用,但选项链的使用更加安全,不会触发一个运行时错误. 在调用一个选项的属性.方法或下标方法时,通过在该选项值的后面放置一个(?)标记来规定一个选项链.这与在选项值后放置一个(!) 来强制展开选项的值非常相似.主要的不同是在选项值为nil时选项链能够

基于Swift语言开发微信、QQ和微博的SSO授权登录代码分析

前言 Swift 语言,怎么说呢,有一种先接受后排斥,又欢迎的感觉,纵观国外大牛开源框架或项目演示,Swift几乎占据了多半,而国内虽然出现很多相关技术介绍和教程,但是在真正项目开发中使用的占据很少部分,原因一是目前熟练它的开发者并不多,二是版本不太稳定,还需要更成熟可靠的版本支持,但总之未来还是很有前景的,深有体会,不管是代码量还是编译效率,以及语言特性,现代性都优于Object-C,估计后续会被苹果作为官方开发语言,值得期待. 走起 鉴于此,笔者将之前用Object-C写的SSO授权登录:微

IOS学习之路——Swift语言(2)——基本类型与函数

一.可选型 为什么会有可选型?当一个变量被希望表示为某一个指定的类型值,同时也被希望在某种情况下表示为空.那么就需要一个可选型.Optional 可选型的语法是在Swift的基本类型后面添加 ? 符号,如 Int? String?  可选型如果取值为空,那么表示为 nil 如果想定义一个可选型的变量,那么需要显示的声明为可选型.可选型变量打印时显示Optional 可选型变量需要进行解包(unwrap)才能与其他的变量进行运算. 解包: 1. 强制解包,使用 ! 关键字,强制解包是具有风险的,当

初步swift语言学习笔记9(OC与Swift杂)

笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/34440159 转载请注明出处 假设认为文章对你有所帮助,请通过留言或关注微信公众帐号fengsh998来支持我,谢谢! swift 语言出来后,可能新的项目直接使用swift来开发.但可能在过程中会遇到一些情况.某些已用OC写好的类或封装好的模块.不想再在swift 中再写一次.哪就使用混编.这个在IOS8中是同意的. 先中简单的入手,先研究在同一个project