Swift初体验
Swift如何导入框架
#import UIKit
#import <UIKit/UIKit.h>
Swift中定义标识符
OC当中定义标识符 : int a = 10 ;
Swift当中定义一个标识符,必须确定该标识符是一个常量还是一个变量
用let 定义常量
用var 定义变量
定义的格式 let /var 标识符名称 : 数据类型 = 赋值
比如 : let /var name : String = "莉莉"
Swift中语句结束时分号问题
在Swift当中如果一行只有一条语句,那么在语句结束时可以不写分号;但一行当中有多条语句的时候,就必须用分号相隔。
Swift当中的常量和变量
在Swift当中定义标识符之前是必须确定常量或变量的
如 : let name : String = "路西"
name = "大卫"
var age : Int = 12
age = 15
Swift中常量变量使用注意
01. 在项目开发中,我们优先定义常量,当我们发现需要改变其值时,再修改成变量。
02. 若使用let定义一个对象时,虽然指针所指向的这个对象的内存地址不能改变,但是可以通过指针获得这个对象,然后改变对象内部属性的值。
如 : var testView : UIView = UIView ()
testView = UIView ()
let testView01 = UIView ()
testView01 = UIView ()
testVaiew01.backgroundColor = UIColor .red
Swift创建对象案例及补充
案例 : 在一个view中添加一个button
import UIKit
let viewRect : CGRect = CGRect(x: 100 , y: 100 , width: 100 , height: 100 )
let testView : UIView = UIView(frame: viewRect)
testView.backgroundColor = UIColor.orange
let testBtn : UIButton = UIButton(type : .custom)
testBtn.frame = CGRect(x: 0 , y: 0 , width: 50 , height: 50 )
testBtn.backgroundColor = UIColor.red
testBtn.setTitle("按钮" , for : .normal)
/**
注意 : Swift中的枚举,如果可以根据上下文推导出枚举的类型,可以直接写 .具体的类型;若推导不出来,就只能写成 枚举类型.具体的类型
如 : testBtn.setTitle("按钮", for: .normal)
testBtn.setTitle("按钮", for: UIControlState.normal)
*/
testView.addSubview(testBtn)
Swift中的类型推导
Swift是一门强类型语言,在定义常量or 变量时必须确定其数据类型,但定义时若进行直接赋值,那么标识符后面写的数据类型课可以省略,因为在Swift中会自动根据后面的赋值推导出常量or 变量的数据类型,可以按住option +鼠标左键查看常量or 变量的数据类型.
let name01 : String = "莉莉"
let name02 = "莉莉"
Swift中的基本运算
Swift中是没有隐式类型推导的,所以在运算时要求所参与运算的数据类型必须一致,不一致的可以进行强制类型转换.
如 : let num1 : Int = 10
let num2 : Double = 1.1
let result = Double(num1) + num2
Swift当中的逻辑分支
Swift中if语句的使用
回顾OC中if 语句的使用,总结Swift中if 的使用区别有两点
01. if 语句后面跟的条件表达式小括号可以省略
02. if 语句后面的条件表达式只接受true or false 类型,没有OC当中非0 即为真和非空即为真的概念
03. if 语句后面只有一条语句时,在OC当中可以省略大括号,但是在Swift当中不可以
例如 :
let score : Int = 59
if score > 100 || score < 0 {
print ("成绩错误" )
} else if score >= 80 {
print ("优秀" )
} else if score >= 60 && score < 80 {
print ("及格" )
} else {
print ("不及格" )
}
Swift中guard语句的使用
guard语句是Swift2.0 推出的,是Swift当中特有的语法 基本格式如下 :
guard 判断语句 else {
else 中的执行语句
}
当判断语句为真的时候,跳过过大括号执行下面的语句,若判断语句为假时,执行else 里面的语句
案例 : 判断能否上网
let age : Int = 18
func isCanPlay(age : Int, money : Bool, isHaveCard : Bool) -> Bool {
guard isHaveCard else {
return false
}
guard money else {
return false
}
guard age >= 18 else {
return false
}
return true
}
let result = isCanPlay(age: 18 , money: true , isHaveCard: true )
if result {
print("可以上网" )
} else {
print("回家吧" )
}
Swift中Switch语句的使用
对比OC中的Switch 语句,Swift当中对其进行了加强和改变.
01. 在Swift当中Switch 后面可以省略小括号,可以省略break 语句
02. 在Swift当中case 语句后面可以跟多个判断条件
let jijie : Int = 3
switch jijie {
case 1 ,2 ,3 :
print ("春季" )
case 4 ,5 ,6 :
print ("夏季" )
case 7 ,8 ,9 :
print ("秋季" )
case 10 ,11 ,12 :
print ("冬季" )
default :
print ("不合理月份" )
}
03. 在Swift当中如果想实现case 穿透效果,只需要手动添加fallthrough语句即可
04. 在Swift中Switch 语句除了可以判断整型以外,还可以判断其他数据类型,比如可以判断浮点型、字符串、区间等
案例一 : 两个数进行加减乘除运算
let num1 : Double = 1.5
let num2 : Double = 9.9
func calculate(num1 : Double, num2 : Double, oprationStr : String) ->Double {
switch oprationStr {
case "+" :
return num1 + num2
case "-" :
return num1 - num2
case "*" :
return num1 * num2
case "/" :
return num1 / num2
default :
return Double(MAXFLOAT)
}
}
var result = calculate(num1: num1, num2: num2, oprationStr: "*" )
print (result)
案例二 : 区间判断成绩
let score : Double = 91
switch score {
case 0. .<60 :
print ("不及格" )
case 60. .<90 :
print ("良" )
case 90. ..100 :
print ("优秀" )
default :
print ("不合法分数" )
}
Swift当中的循环使用
Swift当中的for循环
在OC当中的for 循环基本格式
for (int i = 0 ;i < 10 ;i ++){
}
在Swift2. 0 中for 循环的格式
for (var i = 0 ;i < 10 ;i ++){
}
在Swift3. 0 中for 循环的格式(配合区间使用)
for i int 0. .<10 {
}
或
for i in 0. ..9 {
}
注意 : 在Swift当中如果一个变量没有被使用,可以用‘_’代替,避免不必要的内存占用,比如上面的i 并又有使用,就可以用下滑线替代
for _ in 0. ..9 {
}
Swift中的while循环和do~while循环
01. 在Swift当中while 循环和OC当中的基本一样,但是条件表达式可以省略小括号,只接收bool类型,和if 语句的条件表达式写法一样
var num = 0
while num < 10 {
num += 1
print(num )
}
02. 在Swift当中do ~while 循环 改为 repeat ~while 循环
var num = 0
repeat {
num += 1
print(num )
} while num < 10
注意点 : 在Swift3.0 中 禁用了自增自减运算符 i++ i
Swift当中的字符串
Swift当中字符串的定义
let str01 : String = "莉莉"
var strM01 : String = "莉莉"
strM01 = "路西"
var str : String = String ()
str = "啦啦"
Swift中字符串长度
let name : String = "曹航玮"
print(name.characters.count)
Swift中字符串拼接
let preUrl : String = "https://"
let hasUrl : String = "www.baidu.com"
print(preUrl + hasUrl)
let name : String = "曹航玮"
let score : Double = 99.9
print("\(name)的分数是\(score)" )
let min : Int = 3
let sec : Int = 12
let time = String (format: "%02d : %02d" , min , sec)
print(time)
Swift中字符串的截取
let url : String = "https://www.baidu.com"
let subStr1 = (url as NSString).substring(to : 8 )
let range2 = NSRange(location: 12 , length: 5 )
let subStr2 = (url as NSString).substring(with : range2)
let subStr3 = (url as NSString).substring(from: range2.location + range2.length)
let strIndex = url.index(url.startIndex, offsetBy: 8 )
let subStr01 = url.substring(to : strIndex)
let beginIndex = url.index(url.startIndex, offsetBy: 12 )
let endIndex = url.index(url.startIndex, offsetBy: 17 )
let range = Range(beginIndex..<endIndex)
let subStr02 = url.substring(with : range)
let fromIndex = url.index(url.endIndex, offsetBy: -4 )
let subStr03 = url.substring(from: fromIndex)