Swift学习- 语法1
2021-08-25 本文已影响0人
快乐的tomato
swift和OC的语法还是有很大的不同的,OC转过来的还不太习惯。
1、创建一个ViewController
OC是一个.h 和.m文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
Swift就一个文件
import UIKit
class YuFaStudyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
2、定义变量let、var和自动推导
- let:定义一个常量,一旦设置数值,不允许修改
- var:定义变量,可以修改
- 如何选择:尽量用let,必须要修改的时候,再用Var,程序更安全
- 自动推导:会根据右侧设置的数值,推断常量、变量
在swift中,不会做隐式转换,即不同类型的变量不能直接相加减,swift是一个类型要求很严格的语言 - 句子后面不用带分号
//let是int 64位 long
let a = 10
//let是double 双精度的小数
let b = 1.5
var num = 1.4
num = 1.7
print(num)
let c = a + Int(b)
let n = Double(a) + b
print(c)
print(n)
//创建一个view
let viewOne = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
viewOne.backgroundColor = UIColor.red
view.addSubview(viewOne)//句子后面不用带分号
3、可选项
let x :Double = 1.5 //定义 一个为Double类型的x变量
let x :Double ?= 1.5 //定义 一个为Double类型的x变量,x可以是一个Double型,也可以是nil, 如果是变量,默认为nil
let x :Double != 1.5 //定义 一个为Double类型的x变量,程序员承诺y一定有值,如果没有值,就会崩掉
// ? 可选项:一个变量,可以为本身的类型,也可以为nil
func demo1(){
//定义变量、常量,如果需要指定 那么就是 类型 :类型的方式指定准确的变量
let x :Double = 1.5 //定义 一个为Double类型的x变量
let x :Double ?= 1.5 //定义 一个为Double类型的x变量,x可以是一个Double型,也可以是nil, 如果是变量,默认为nil
let x :Double != 1.5 //定义 一个为Double类型的x变量,程序员承诺y一定有值,如果没有值,就会崩掉
print(x + 2.5)
//可选项 使用?定义
//y可以是一个整数/也可以是nil,如果是变量,默认为nil
//可选项在输出的时候,会提示Option
// var y:Int = 10
var y:Int?
print(y)
/*
可选项不能直接计算
!:强行解包:程序员承诺y一定有值,如果没有值,就会崩掉
每次写 !都有考虑是不是有空值
*/
print(y!+10)
let urlstr = "http//www.kk.com";
//构造函数如果有?,表示不一定能够创建出对象,
//如果参数中没有?,表示必须有值,如果为nil,就崩
let url = NSURL(string: urlstr)
print(url)
}
4、if分支和三目
/*
1、if 条件后面没有小括号,必须有{}
2、C语言中有一个非零即真,swift中只有false / true
*/
func demo2(){
let x = 20
if x > 10 {
print("大了")
}else{
print("小了")
}
//三目,在swift中用的很多
x>10 ? print("大了"):print("小了")
}
5、if let 语法
![](https://img.haomeiwen.com/i1784598/2c13055c845d7442.png)
6、guard
![](https://img.haomeiwen.com/i1784598/5368775995a99cd3.png)
7、switch
![](https://img.haomeiwen.com/i1784598/97cb02afebf1d650.png)
8、可选项的默认值
![](https://img.haomeiwen.com/i1784598/548d9fd9cec4de67.png)
9、字符串
![](https://img.haomeiwen.com/i1784598/f3b7d7e82b750cb1.png)
//拼接字符串的方式
let name = "张三"
let age = 18
print("\(name) \(age)")
//字符串的遍历
let str = "我是中国人"
for c in str {
print(c)
}
//返回字符串的字符数量
print(str.count)
//字符串的子串,取子串不是特别好写,建议使用NSString
let s1 = (str as NSString).substring(with: NSMakeRange(1, 3))
print(s1)
10、数组
![](https://img.haomeiwen.com/i1784598/56c4fa9fe771abde.png)
![](https://img.haomeiwen.com/i1784598/9694c3649b02ae21.png)
![](https://img.haomeiwen.com/i1784598/ab7fd5d92b641867.png)
![](https://img.haomeiwen.com/i1784598/37e75153b6aecef0.png)
![](https://img.haomeiwen.com/i1784598/2f4fca49fd715b91.png)
![](https://img.haomeiwen.com/i1784598/6f30fba98dd86c5a.png)
![](https://img.haomeiwen.com/i1784598/519b21a504dba605.png)
11、分割标志
![](https://img.haomeiwen.com/i1784598/e016c40b6e9890c9.png)
12、字典
![](https://img.haomeiwen.com/i1784598/657f3bf3efb95c29.png)
字典的遍历
![](https://img.haomeiwen.com/i1784598/59d0fb97de9449a0.png)
字典的组合
![](https://img.haomeiwen.com/i1784598/e58a639dba534310.png)
13、函数
![](https://img.haomeiwen.com/i1784598/69315ac664cb58f8.png)
![](https://img.haomeiwen.com/i1784598/20c39e8b40347fa7.png)
![](https://img.haomeiwen.com/i1784598/5190018e2d291151.png)
![](https://img.haomeiwen.com/i1784598/d469be21a36aba74.png)