学习swift的笔记,偶尔会更新一下
变量定义使用var
,常量使用let
,类型安全,有自动类型推导,注意赋值的=
号两边必须有空格。
变量和常量名是可以几乎所有字符,这些都非常像javascript。中文编程一下牛逼了。
var a = 123 //a为Intlet b = "helo" //b为Stringvar 猫叫 = "喵"
0b101
0o5
0x5
比较长的数字间可以加上_
用来提高程序的可读性,比如0_0
其实就是0,_
线不能加在开头
true
和false
,流程控制if
的时候,判断语句返回必须是一个Bool
值,比如:
let i = 1if i { //编译报错}
这样就可以通过
if i == 1 {}
它不像js里会有自动类型转换
给现在的类型添加别名,同样可以提高程序的可读性,如
typealias 音频采样 = UInt16
可以在别的地方使用
var 已发现的最大振幅 = 音频采样.min
它可以是一组值,这些值不必是相同的类型,例如,定义我自己:
var jserme = ("183cm", 26, "76kg")
可以像数组一样访问
PRintln(jserme.0) //返回183cm
元组还原为独立的变量或者常量
let jserme = ("183cm",26,"76kg")let (身高, 年龄, 体重) = jsermeprintln("身高是 /(身高)")
也可以给每个值命名(这就像在JS里把数组搞成了对象了。。。)
let jserme = (身高:"183cm",年龄:26,体重:"76kg")println("身高是 /(jserme.身高)")
字符串字面量只能用""
来定义,String
本质上是Character
的有序集合。
for char in "一言既出"{ println(char)}/*一言既出*/
字面量与判断是否为空
var 字符串 = "我是字符串"var 空字符串 = ""if 空字符串.isEmpty { println("这是一个空的字符串")}if 空字符串 == "" { println("这是一个空的字符串")}
字符串实例有两个方法hasprefix
与hasSuffix
,如:
var 成语数组 = [ "一言既出", "一触即发", "一呼百应", "一槌定音", "一无所有", "一生一世", "一见钟情"]var count = 0for 成语 in 成语数组 { if(成语.hasPrefix("一")){ count++ }}println(count) //输出7
与js一样,string也是传值引用,下面的两个变量的修改不会影响到彼此
var 一串 = "我是字符串一"var 二串 = 一串二串 = "我是字符串二"println("字符串一:/(一串), 字符串二:/(二串)")
闭区间使用a...b
,从a到b,包含a与b,半区间a..b
,从a到b,不包含b,例如:
var 成语数组 = [ "一言既出", "一触即发", "一呼百应"]for i in 0..成语数组.count { println("第/(i)个成语是:/(成语数组[i])")}//这里如何使用...会报错,因为成语数组[3]是没有值的
相对于js对数组和对象成员松散的要求,swift要求数组和dictionaries里成员类型必须一致
var 购物清单: String[] = ["鸡蛋", "牛奶"]//也可以是下面的这样//var 购物清单 = ["鸡蛋", "牛奶"]
数组的修改可以使用append
方法或者+=
var 购物清单 = ["鸡蛋", "牛奶"]购物清单.append("苹果")购物清单 += "草莓"println("/(购物清单)") //[鸡蛋, 牛奶, 苹果, 草莓]
数组的获取,可以通过索引,也可以通过区间运算符
var 购物清单 = ["鸡蛋", "牛奶"]println("/(购物清单[0])") //鸡蛋println("/(购物清单[0..1])") //[鸡蛋]println("/(购物清单[0...1])") //[鸡蛋, 牛奶]println("/(购物清单[0...2])") //[鸡蛋, 牛奶, ]
dictionaries的定义
var airports: Dictionary<String, String> = ["TYO": "Tokyo", "DUB": "Dublin"]//也可以简化为//var airports = ["TYO": "Tokyo", "DUB": "Dublin"]
它的修改与读取使用[]
,而不能使用.
airports["BJ"] = "Beijin"
如前面的几个例子所示,控制语句的条件不像js有小括号
for var index = 0; index < 3; index++ { println("index is /(index)")}//index is 0//index is 1//index is 2
函数的声明与调用:
func sayHello(personName: String) -> String { let greeting = "Hello, " + personName + "!" return greeting}println(sayHello("jserme"))
无返回的函数,其实质是返回一个Void
,它等同于一个空的元组()
多返回值的函数与默认参数:
func info(Word:String = "aha") -> (length:Int, containA:Bool){ var containA = false for char in word { if( char == "a") { containA = true break } } return (word.utf16count, containA)}println(info(word: "波波")) //(2, false)println(info()) //(3, true)
便于阅读的外部参数名,在参数定义之前,与参数定义以空格隔开,如下面的多个参数
func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String { return s1 + joiner + s2}//调用的时候join(string: "hello", toString: "world", withJoiner: ", ")// returns "hello, world"
参数名与外部参数名一致,可以给参数名加#
标识:
func containsCharacter(#string: String, #characterToFind: Character) -> Bool { for character in string { if character == characterToFind { return true } } return false}let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v")// containsAVee equals true, because "aardvark" contains a "v"
函数的参数是常量,不可以修改,如果在函数内修改,变量定义前加var
func alignRight(var string: String, count: Int, pad: Character) -> String { let amountToPad = count - countElements(string) for _ in 1...amountToPad { string = pad + string } return string}let originalString = "hello"let paddedString = alignRight(originalString, 10, "-")// paddedString is equal to "-----hello"// originalString is still equal to "hello"
如果想在函数内修改传入的参数,可以使用inout
关键字来标识,传入的参数需要前缀&
,这内部实现应该是指针。
func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA}var someInt = 3var anotherInt = 107swapTwoInts(&someInt, &anotherInt)println("someInt is now /(someInt), and anotherInt is now /(anotherInt)")// prints "someInt is now 107, and anotherInt is now 3"
函数类型,可以像js一样使用函数作为参数及返回值
func addTwoInts(a: Int, b: Int) -> Int { return a + b} //函数类型为 (Int, Int) -> Intfunc multiplyTwoInts(a: Int, b: Int) -> Int { return a * b}//函数类型为 (Int, Int) -> Int//接收名为mathFunction的函数类型func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { println("Result: /(mathFunction(a, b))")}printMathResult(addTwoInts, 3, 5)// prints "Result: 8"//返回函数类型func stepForward(input: Int) -> Int { return input + 1}func stepBackward(input: Int) -> Int { return input - 1}func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward}var currentValue = 3let moveNearerToZero = chooseStepFunction(currentValue > 0)// moveNearerToZero now refers to the stepBackward() function
函数与它包含的上下文的变量在一起称为闭包。如sort
函数:
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]func backwards(s1: String, s2: String) -> Bool { return s1 > s2}var reversed = sort(names, backwards)println(reversed)// reversed is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]s
使用闭包可以表示为:
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]var reversed = sort(names, {(s1:String, s2:String) -> Bool in return s1 > s2})println(reversed)// reversed is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
也可以简化为
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]var reversed = sort(names, { s1, s2 in s1 > s2 } )println(reversed)
通过下面的语法声明
enum Barcode { case UPCA(Int, Int, Int) = (1,2,3) case QRCode(String) = "hello"}
推荐使用首字母大写来命名
struct Resolution { var width = 0 var heigth = 0}class VideoMode { var resolution = Resolution() var interlaced = false var frameRate = 0.0 var name: String?}
生成实例:
let someResolution = Resolution()let someVideoMode = VideoMode()
属性访问与修改,使用.
语法:
println("The width of someVideoMode is /(someVideoMode.resolution.width)")someVideoMode.resolution.width = 12880println("The width of someVideoMode is now /(someVideoMode.resolution.width)")
结构体有自动成员初始化器,类实例没有:
let vga = resolution(width:640, heigth: 480)
结构体与枚举都是值类型,类是引用类型
对于引用了同一个实例的值,可以使用===
和!==
来进行判断
延迟属性,@lazy
,设置在调用的时候才初始化特定的属性
class DataImporter { /* DataImporter 是一个将外部文件中的数据导入的类。 这个类的初始化会消耗不少时间。 */ var fileName = "data.txt" // 这是提供数据导入功能}class DataManager { @lazy var importer = DataImporter() var data = String[]() // 这是提供数据管理功能}let manager = DataManager()manager.data += "Some data"manager.data += "Some more data"// DataImporter 实例的 importer 属性还没有被创建
类、结构体、枚举都可以通过设置setter
与getter
来
struct AlternativeRect { var origin = Point() var size = Size() var center: Point { get { let centerX = origin.x + (size.width / 2) let centerY = origin.y + (size.height / 2) return Point(x: centerX, y: centerY) } set { //这里setter 没有定义表示新值的参数名,则可以使用默认名称newValue origin.x = newValue.x - (size.width / 2) origin.y = newValue.y - (size.height / 2) } }}
只读属性去掉get
与set
属性监视可以使用willset
和didset
来处理
类型属性有点像静态变量,以static
关键字声明
struct SomeStructure { static var storedTypeProperty = "Some value." static var computedTypeProperty: Int { // 这里返回一个 Int 值 }}
类、结构体、枚举都可以有下标
,它有像给它们增加了一个快捷方式,如下:
struct TimesTable { let multiplier: Int subscript(index: Int) -> Int { return multiplier * index }}let threeTimesTable = TimesTable(multiplier: 3)println("3的6倍是/(threeTimesTable[6])")// 输出 "3的6倍是18"
定义一个类
class Vehicle { var numberOfWheels: Int var maxPassengers: Int func description() -> String { return "/(numberOfWheels) wheels; up to /(maxPassengers) passengers" } init() { numberOfWheels = 0 maxPassengers = 1 }}
继承类
class Bicycle: Vehicle { init() { super.init() numberOfWheels = 2 }}
重写属性与方法
class Car: Vehicle { var speed: Double = 0.0 override var speed: Double { get { return super.speed } set { super.speed = min(newValue, 40.0) } } init() { super.init() maxPassengers = 5 numberOfWheels = 4 } override func description() -> String { return super.description() + "; " + "traveling at /(speed) mph" }}
防止重写,在方法与属性前加关键字@final
,编译时会出错
声明里可以写多个init
,这有点像重载
struct Celsius { var temperatureInCelsius: Double = 0.0 init(fromFahrenheit fahrenheit: Double) { temperatureInCelsius = (fahrenheit - 32.0) / 1.8 } init(fromKelvin kelvin: Double) { temperatureInCelsius = kelvin - 273.15 }}let boilingPointOfWater = Celsius(fromFahrenheit: 212.0)// boilingPointOfWater.temperatureInCelsius 是 100.0let freezingPointOfWater = Celsius(fromKelvin: 273.15)// freezingPointOfWater.temperatureInCelsius 是 0.0”
有些地方叫反初始化,很别扭的名字哦
class Player { var coinsInPurse: Int init(coins: Int) { coinsInPurse = Bank.vendCoins(coins) } func winCoins(coins: Int) { coinsInPurse += Bank.vendCoins(coins) } deinit { Bank.receiveCoins(coinsInPurse) }}var player = Player(coins:200)player = nil //调用deinit方法
对于类、结构体、枚举,可以扩展它们的一切
class Player{ var age:Int}extension Player{ func repetitions(task: () -> ()) { for i in 0..self { task() } }}
其实就是接口描述
protocol SomeProtocol { var mustBeSettable: Int { get set } var doesNotNeedToBeSettable: Int { get } func someTypeMethod() }
协议继承
protocol InheritingProtocol: SomeProtocol, AnotherProtocol { // protocol definition goes here }
这个函数的泛型版本使用了节点类型命名(通常此情况下用字母T来表示)来代替实际类型名(如Int、String或Double)。节点类型名并不是表示T必须是任何类型,但是其规定a和b必须是同一类型的T,而不管T表示任何类型。只有swapTwoValues函数在每次调用时所传入的实际类型决定了T所代表的类型。
func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA }
这里演示重载+
号运算符
struct Vector2D { var x = 0.0, y = 0.0}@infix func + (left: Vector2D, right: Vector2D) -> Vector2D { return Vector2D(x: left.x + right.x, y: left.y + right.y)}
@prefix
@postfix
@assignment
@infix
@prefix @assignment func ++ (inout vector: Vector2D) -> Vector2D { vector += Vector2D(x: 1.0, y: 1.0) return vector}
个性的运算符只能使用这些字符 / = - + * % < >!& | ^。~
Operator prefix +++ {}@prefix @assignment func +++ (inout vector: Vector2D) -> Vector2D { vector += vector return vector}
结合性(associativity)的值默认为none
,可用left
,right
,none
,优先级(precedence)默认为100
。
operator infix +- { associativity left precedence 140 }func +- (left: Vector2D, right: Vector2D) -> Vector2D { return Vector2D(x: left.x + right.x, y: left.y - right.y)}let firstVector = Vector2D(x: 1.0, y: 2.0)let secondVector = Vector2D(x: 3.0, y: 4.0)let plusMinusVector = firstVector +- secondVector// plusMinusVector 此时的值为 (4.0, -2.0)
新闻热点
疑难解答