一 playground
1 练习swift是在playground中编写
—-> 1.1 导入框架(swift和oc的区别)
#import <Foundation/Foundation.h>
import Foundation
import UIKit
—-> 1.2 定义标识符
—-> 注意:在swift中如果定义一个标识符,必须告诉编译器该标识符是变量还是常量
—–> 1.2.1 如果是常量使用let修饰
double s = 2.22 ;
let b : Float = 1.2 ;
b = 2.22 ;
—–> 1.2.2 如果是变量使用var修饰
int a = 10 ;
a = 19 ;
var a : Int = 10 ;
a = 19 ;
—-> 1.3 如果一行中只有一条语句,那么语句结束后,可以不加分号(;)
let c : Int = 10
var f : Double = 3.33
—-> 1.4 在swift中如何打印内容?
int g = 10 ;
NSLog(@"%d" ,g);
let d : Int = 100
var j : Double = 100.1
print (d)
print (j)
二 常量&变量
1 常量:定义完后不能修改(let开头)
let a : Int = 10
let s : Double = 2.22
let b : Float = 1.2
a = 11
s = 1.11
b = 1.23
—-> 1.1 注意一:在开发中,apple建议优先使用常量,只有发现需要修改时再改成变量
—-> 1.2 注意二:常量的本质,指向的内存地址不可以修改,但是可以通过内存地址获得到对象,之后修改对象内部的属性
2 变量
var c : Int = 10
var f : Double = 11.2
var g : Float = 1.1
c = 11
f = 10.1
g = 1.2
3 注意:苹果推荐使用变量来定义
三 在swift中创建对象(补充)
1 创建view对象
2 修改view内部属性
3 创建view的子控件btn
—-> 3.1 设置按钮的文字和类型
4 将btn添加到view中
let rect = CGRectMake(0 , 0 , 100 , 100 )
let view : UIView = UIView (frame:rect)
view.backgroundColor = UIColor .yellowColor ()
view.alpha = 0.5
let rectButton = CGRectMake(50 , 50 , 50 , 50 )
let button : UIButton = UIButton (frame: rectButton)
button.backgroundColor = UIColor .redColor ()
view.addSubview (button)
四 swift中的数据类型
1 整型: int
let e : Int = 10
var f = 11
2 浮点型: double(默认) ;float
let a : Double = 1.2
let b : Float = 1.22
var c = 1.1
var d = 1.22
五 swift中的类型推导
1 如果定义一个标识符时,有直接赋值的话,那么可以通过赋值的类型推导出标识符的类型
let a = 10
var c = 11
let b = 1.2
var e = 1.3
let rect = CGRectMake(10 , 10 , 10 , 10 )
let button = UIButton(frame: rect)
button.backgroundColor = UIColor.redColor()
var sw = UISwitch()
sw.setOn(false , animated: true )
六 swift进行基本运算
1 oc中的隐式装换:在int类型和double类型进行计算的时候,会自动将int类型转换为double类型
int a = 12 ;
double b = 11.2 ;
double result = a + b;
int result = a + b;
NSLog (@"%f" ,result);
2 swift中如果要进行基本运算的话,那么就必须保证类型一致,否则是无法进行运算的
—-> 2.1 类型一致
//swift的基本运算(类型一致)
let a = 10
let b = 11
let result = a + b
let result1 = a * b
print (result)
—-> 2.2 类型不一致
let c = 11
let d = 1.2
let result2 = c + d
七 逻辑分支
1 if的使用
—-> 1.1 swift和oc中对if的区别
–OC中if语句
//oc中的if 语句一
/*
int a = 8
if (a > 0 ){
NSLog(@"大于0" )
}else {
NSLog(@"小于0" )
}
*/
/*
int b = 10 ;
if (b){
NSLog(@"大于0" );
}else {
NSLog(@"小于0" );
}
*/
—> 1>swift中if后面的()可以省略
let a = 9
if a > 0 {
print ("大于0" )
}else {
print ("小于0" )
}
—> 2>没有非0即真,必须有明确的Bool–>true/false
// swift中的if 语句二(这是错误的写法)
// swift中的if 语句后面的判断语句没有非0 即真,必须明确的标识false 或者ture
/*
let b = 10
if b {
print ("大于0" )
}else {
print ("小于0" )
}
*/
2 通过if…else if来判断一个分数是否合格(和if的用法基本是一致的)
let scroe = 100
if scroe < 0 || scroe > 100 {
print ("分数不合法" )
}else if scroe < 60 {
print ("不及格" )
}else if scroe < 80 {
print ("及格" )
}else if scroe < 90 {
print ("良好" )
}else {
print ("优秀" )
}
3 三目运算符
—-> 1>判断两个数的最大值(if和三目运算)—>三目运算符和oc完全一致
oc中的三目运算
//oc中的三目运算
/*
int a = 10 ;
int b = 9 ;
int result = a > b ? a : b;
NSLog(@" %d ",result);
*/
swift中的三目运算
//swift中用三目运算来求结果
let c = 10
let d = 11
let result = c > d ? c : d;
print ("两者最大值为" ,result)
4 guard的使用(重点)—>判断一个人是否可以上网
—-> 1>任何用if的地方都能用guard,反之也成立.但是guard可以提高代码的可读性,比if好用
—-> 2>guard语句必须带else
—-> 3>guard的使用
—-> 3.1 如果条件表达式为真,则会执行后面的语句
—-> 3.2 如果条件表达式为假,则会执行else后面的{}语句
let age = 20
func onLine(age : Int){
guard age > 18 else {
print ("不能上网" )
return
}
guard 带了身份证 else {
print ("不能上网" )
return
}
guard 有电脑 else {
print ("不能上网" )
return
}
print ("开机上网" )
}
—-> 4 >补充(guard在国内翻译为:守卫)
5 switch分支(swift和oc的区别)
oc中的 switch
// oc中switch 语句的使用
/*
int a = 10 ;
switch (a){
case 10 :
NSLog(@"等于10" );
break ;
case 11 :
NSLog(@"大于10" );
break ;
case 9 :
NSLog(@"小于10" );
break ;
}
*/
—-> 5.1 switch后面的()可以省略
—-> 5.2 case结束后可以不加break,系统会默认跟上break
// switch 在swift中的使用(一)---> switch 后面的括号可以省略,可以不加break ,系统会默认跟上break
let a = 11
switch a {
case 10 :
print ("等于10" )
case 11 :
print ("大于10" )
case 9 :
print ("小于10" )
default :
print ("其它" )
}
—-> 5.3 switch基本用法的补充 1> case后面跟多个条件 2>如果case中希望产生case穿透,可以使用fallthrough
let b = 0
switch b {
case 1 :
print ("女" )
case 2 :
print ("男" )
fallthrough ;
default :
print ("其它" )
}
// switch 在swift中的使用(四)---> case 后面可以跟上多个条件
let e = 0
switch e {
case 0 ,1 :
print ("人" );
default :
print ("其它" )
}
—-> 5.4 swift支持浮点型
// switch 在swift中的使用(四)---> 支持浮点型
let c = 1.2
switch c {
case 1.2 ,1.3 :
print ("人" )
default :
print ("其它" )
}
—-> 5.5 swift支持字符串类型
// switch 在swift中的使用(三)--> 支持字符串类型
let 字符串 = "测试字符串"
switch 字符串 {
case "测试" :
print ("测试就是两个字!" )
break
case "字符串" :
print ("字符串能吃吗?" )
break
case "位置" :
print ("不知道耶。。" )
break
case "测试字符串" :
print ("终于对啦!" )
break
case let 测试:
print ("呵呵..没有default吧?" )
break
}
—-> 5.6 swift支持区间类型(区间指的是:数字之间的区间)
let score = 80
switch score {
case 0 ..< 60 :
print ("不及格" )
case 60 ..< 80 :
print ("及格" )
case 80 ..< 90 :
print ("良好" )
case 90 ... 100 :
print ("优秀" )
default :
print ("分数不合法" )
}
八 循环
1 打印出0~9
—-> 1.1 oc写法(for循环)
/*
for (int i = 0 ;i < 10 ;i++){
NSLog(@"%d" ,i);
}
*/
—-> 1.2 swift写法(三种写法)
// swift中的循环写法(一)--> 注意: "..<" 是一块的,中间不能隔开
for i in 0 ..< 10 {
print (i)
}
// swift中的循环写法(二)--> 注意: "i++{" 这样挨着写就会报错
for var i = 0 ; i < 10 ; i++ {
print (i)
}
//swift中的循环写法(三)-->如果中间的i并没有起到作用,可以用下划线代替
for _ in 0 ..< 10 {
print("hellow world" )
}
for _ in 0 ... 10 {
print("hellow swift" )
}
2 while和do…while循环
—-> 2.1 swift中的while和oc的区别1> while后面的()可以省略 2>没有非0即真
—-> 2.2 oc中的while和do…while循环
//oc中的whil使用
/*
while (条件表达式){
NSLog(@"满足条件进入" );
}
*/
//oc中的do ...while 使用
/*
do {
NSLog(@"总会先执行一次,然后在判断while后面的表达式" );
}while (条件表达式);
*/
—-> 2.3 将do换成repeat
//swift中的while 使用(一)
var a = 13
while (a > 2 ){
print("2" )
a --
}
//swift中的while 使用(二)
var b = 13
repeat {
print ("2" )
b
}while b > 0
九 字符串
1 使用let定义的字符串是不可变的
//不可变字符串
let a = "adc"
print(a )
a = "asv" 错误写法
2 使用var的字符串是可变的
var b = "adc"
b = "ghj"
print (b)
3 获取字符串的长度
var c = "adcv"
let length = c.characters.count
print(length)
4 遍历字符串中所有的字符
// 遍历字符串中所有的字符--> e.characters类似于拿到字串中所有的字符
let e = "addafafda"
for d in e.characters {
print (d)
}
5 字符串的拼接 1>两个字符串之间的拼接2>字符串和其它标识符之间的拼接
let str = "xiaofeng"
let str1 = "肖锋"
let str2 = str + str1
let name = "肖锋"
let age = 19
let height = 1.88
let togetherInfo = "my name is \(name),age is \(age), height is \(height)"
print(togetherInfo)
6 字符串的格式化
let totalSecond = 64
let min = totalSecond / 60
let second = totalSecond % 60
let timeString = String (format: "%02d:%02d" , arguments: [ min , second] )
7 字符串的截取 1>将string类型转成NSString类型2>swift的截取方式
—-> 7.1 字符串的截取
var urString = "www.baidu.com"
let str1 = (urString as NSString).substringToIndex(3 )
let str2 = (urString as NSString).substringFromIndex(10 )
let range = NSMakeRange(4 , 5 )
let middle = (urString as NSString).substringWithRange(range)
7.2 swift的截取方式
let toIndex = urString.startIndex.advancedBy(3 )
let heard1 = urString.substringToIndex(toIndex)
let fromIndex = urString.endIndex.advancedBy(-3 )
let footer1 = urString.substringFromIndex(fromIndex)
let range1 = Range(start: toIndex.advancedBy(1 ), end :fromIndex.advancedBy(-1 ))
let middle1 = urString.substringWithRange(range1)
十 数组
1 如何定义一个数组
—-> 1.1 定义不可变数组:使用let修饰
let array = ["adc" ]
print (array )
—-> 1.2 定义可变数组:使用var修饰
var array1 = ["adf" ]
array1 = ["aefgg" ]
print (array1)
2 对可变数组的基本操作(增删改查)
—-> 2.1 增
var array2 = ["xiaofeng" ]
array2.append ("nb" )
array2.append ("it" )
—-> 2.2 删
var array3 = ["xaiofeng" ]
array3.removeAtIndex(0 )
print (array3)
—-> 2.3 改
var array4 = ["xiaofeng" ]
array4.append ("nb" )
array4[1 ] = "it"
print (array4)
—-> 2.4 查
var array5 = ["xiaofeng" ]
array5.append ("it" )
array5[0 ]
print (array5[1 ])
3 遍历数组(四种方式)—>方式二和方式三比较常见
—-> 3.1 方法一
var array6 = ["xaiofeng" ]
array6.append ("it" )
array6.append ("nb" )
for i in 0 ..< array6.count {
print (i)
print (array6[i])
}
—-> 3.2 方法二
var array7 = ["xaiofeng" ]
array7.append ("it" )
array7.append ("nb" )
for name in array7 {
print (name)
}
—-> 3.3 方法三
var array8 = ["xaiofeng" ]
array8.append ("it" )
array8.append ("nb" )
for (index, name) in array8.enumerate() {
print (index)
print (name)
}
—-> 3.4 方法四
var array9 = ["xaiofeng" ]
array9.append ("it" )
array9.append ("nb" )
for name in array9[0 ..< 3 ] {
print (name)
}
4 数组的合并(如果两个数组的类型一致,那么可以相加进行合并)
let name1 = ["xiaofeng" ,"it" ]
let name2 = ["nb" ]
let name3 = ["xiaofeng" ,"it" ,18 ,18.9 ]
let names = name1 + name2
十一 字典
1 定义字典
—-> 1.1 定义不可变字典:使用let修饰
let dict = [
"name" :"xiaofeng" ,
"age" :18 ,
"height" :180
]
//dict = [ "career" :"it" ] 不可变字典,不能修改
—-> 1.2 定义可变字典:使用var修饰
var dict1 = [String :NSObject]()
2 对可变字典的基本操作(增删改查)
—-> 2.1 增
var dict2 = [String :NSObject]()
dict2["name" ] = "xiaofeng"
dict2.updateValue(18 , forKey: "age" )
—-> 2.2 删
dict2.removeValueForKey("age" )
—-> 2.3 改
//修改元素
dict2["name" ] = "xiao"
dict2
dict2.updateValue("xiaofeng" , for Key: "name" )
dict2
—-> 2.4 查
dict2["name" ]
3 遍历子字典(三种)
—-> 3.1 第一种
for key in dict2.keys {
print (key)
}
—-> 3.2 第二种
for value in dict2.values {
print(value )
}
—-> 3.3 第三种
for (key,value ) in dict2 {
print(key)
print(value )
}
4 合并字典:相同类型也不可以合并
let dict3 = [
"name" :"xiaofeng" ,
"age" :18 ,
"career" :"it"
]
var dict4 = ["height" :1.99 ,"phoneNume" :"119" ]
for (key,value ) in dict3 {
dict4.updateValue(value , forKey: key)
}
print(dict4)
十二 元组
1 例子:定义一组数据,用于描述个人信息
—-> 1.1 使用元组来描述
—-> 1.2 元组的基本使用(定义)
let infoTuple = ("xiaofeng" , 20 , 1.90 )
infoTuple.0
infoTuple.1
infoTuple.2
—-> 1.3 给元组起别名
//给元组中每一个元素起别名
let infoTuple1 = (name : "xiaofeng" , age : 20 , height : 1.90 )
infoTuple1.name
infoTuple1.0
infoTuple1.age
infoTuple1.1
infoTuple1.height
infoTuple1.2
—-> 1.4 解释别名含义
//别名就是元组对应标识符的名称
let (name, age, height) = ("xiaofeng" , 20 , 1.90 )
name
age
height
十三 可选类型(难点)
1 可选类型推出的目的:对nil值进行处理
2 定义可选类型
var name : Optional<String > = nil
3 给可选类型赋一个具体值
print (name)
name = "xiaofeng"
print (name)
4 从可选类型中取出具体的类型 强制解包: 可选类型 + !
print (name!)
5 可选类型的注意: 如果可选类型中没有值,那么强制解包程序会崩溃
if name != nil {
print (name!)
}
—-> 建议: 在对可选类型进行解包之前,先判断可选类型是否等于nil
6 可选绑定
—-> 6.1 判断name是否有值,如果没有值,直接不执行{}
—-> 6.2 如果name有值,那么系统会对name进行解包,并且将解包的结果赋值
if let tempName = name {
print (tempName)
}
if let name = name {
print (name)
}
十四 总结
1 今天为大家带来的是swift语言的简单入门,里面讲的也还算详细,希望能给一些想要学习的it从业者一点启发吧.尽量的帮助大家.明天还是会时时的更新有关swift的知识.最后如果大家觉得我写的博客还满意的话,麻烦大家关注我的官方博客,谢谢!!!!