Swift

iOS开发 -- Swift之基本类型(一)

2016-09-07  本文已影响0人  Sorphiean

这一系列的文章,主要是总结比较了Swift与OC的不同之处,以及Swift新添加的特性,方便使用OC开发iOS的工程师可以快速上手Swift。

常量、变量和声明

常量和变量

//常量 let
let maxNum = 1000
//变量 var
var index = 2
注意:
var a = 10 , b = 20 ,c = 30
a = "Hello"    
//报错:Cannot assign value of type 'string' to type 'Int'
let str: String = "Hello"
var a, b, c: Double

常用基本类型

Int和UInt

Int.max             //9223372036854775807
Int.min             //-9223372036854775808

UInt.max            //18446744073709551615
UInt.min            //0
var num: Int
num = 99999999999999999999999
//error: integer literal '99999999999999999999999' overflows when stored into 'Int'

var num: UInt
num = -19
//error: negative integer '-19' overflows when stored into unsigned type 'UInt'
var num: Int = 1_000_000    //等同于Int = 1000000

Float和Double

let imFloat: Float = 3.1415926      //3.14159
let imDouble: Double = 3.1415926    //3.1415926
let x = 3.1415926       //let x: Double
let x = 1.25e10         //12500000000

let y = 123_000.000_001 //123000.000001
let a:Int16 = 16
let b:Int8 = 8
a + b
//error: binary operator '+' cannot be applied to operands of type 'Int16' and 'Int8'
//可以通过手动强转解决
a + Int16(b)            //24


let a:Double = 3.0
let b:Float = 0.3
a + b
//error: binary operator '+' cannot be applied to operands of type 'Double' and 'Float'
//可以通过手动强转解决
a + Double(b)            //3.3


//同理,小数与整数也不能直接运算
let a = 3
let b = 0.3
a + b
//error: binary operator '+' cannot be applied to operands of type 'Int' and 'Double'


let red = 0.3
let green = 0.5
let blue = 0.7
UIColor(red: red, green: green, blue: blue, alpha: 1)
//error: cannot convert value of type 'Double' to expected argument type 'CGFloat'

let x:UInt16 = 1500
let y:UInt8 = 20

let n = UInt8(x) + y
//Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

Boolean和简单的if语句

let imTure = true
let imFalse = false

if imTure {
    print("I'm true")
} else {
    print("I'm false")
}
let imTure = true
let imFalse = false

if imFalse {
    print("I'm true")
}
else if 3 + 4 == 7 {
    print ("3 + 4 == 7")
}
else {
    print ("I'm false")         //Will never be executed
}
if 1 {
    print("1")      //error: type 'Int' does not conform to protocol 'BooleanType'
}

Tuple

元组,将多个数据放到一个数据类型中,有点类似C语言中的结构体

var point = ( 5 , 2 )
var httpResponse = ( 404 , "Not Found" )
var point2: ( Int , Int , Int ) = ( 10 , 5 , 2 )
var httpResponse2: ( Int , String) = ( 200 , "OK" )
//方法一
var point = ( 5 , 2 )
let ( x , y ) = point
x               //5
y               //2

//方法二
var point = ( 5 , 2 )
point.0         //5
point.1         //2
//以上方式并不直观,Swift允许我们给元组内各个元素定义一个名称
//方法三
let point3 = ( x:3 , y:2 )
point3.x        //3
point3.y        //2

let point4:( x : Int , y : Int ) = ( 5 , 6 )
point4.x        //5
point4.y        //6
let loginResult = ( true , "wangxuean" )
let (isLoginSuccess , _ ) = loginResult         //使用下划线来忽略一些我们不关心的值
if isLoginSuccess {
    print("Login Success!")
}
else {
    print("Login Failed!")
}

章节补充

let 🐅 = "老虎"
let 名字 = "wangxuean"
print ("我的名字是" + 名字)     //我的名字是wangxuean
print ("Hello")

let x = 1 , y = 2 , z = 3
print( x , y , z )                  //1 2 3
print( x , y , z ,separator:",")    //1,2,3
//默认的separator是空格
print( x , y , z ,separator:"," , terminator : "!")    //1,2,3!
//默认的terminator是回车,自定义修改后没有回车的功能,需要则要自己标注


print ( y , "*" , z , "=" , y*z)        //不方便
//字符串插值
print ("\(y) * \(z) = \(y*z) ")         //2 * 3 = 6
上一篇下一篇

猜你喜欢

热点阅读