前端之家收集整理的这篇文章主要介绍了
Swift初级:变量与数据类型,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//: Playground - noun: a place where people can play
import UIKit
//1.第一个程序
var str = "Hello,playground"
println("Hello,swift!")
//2.let 常量constants, var 变量variables
let webSiteName:String = "www.baidu.com"
var 整型:Int = 10
let bignum_a = 1000000
let bignum_b = 1_000_000
let bignum_c = 1_0000_0000
var 浮点型64:Double = 10.0,浮点型32:Float = 10.0
var red,green,blue:Double
//强制类型转换
let integer_a:Int = 3
let double_b:Double = 0.1415926
let pi = Double(integer_a) + double_b
//3.布尔类型以及if语句
let imtureVar:Bool = true
let imfalseVar:Bool = false
if imtureVar {
println(imtureVar);
}else{
println(imfalseVar);
}
//4.Tuples 元组
let registerResult = (isRegisterSuccess:true,nickname:"慕课女神",gender:"女")
//遍历元组1
let (isRegisterSuccess,nickname,gender) = registerResult
isRegisterSuccess
nickname
gender
//遍历元组2
registerResult.0
registerResult.1
registerResult.2
//遍历元组3
registerResult.isRegisterSuccess
registerResult.nickname
registerResult.gender
//使用下划线(_)忽略部分数值
let connectionResult : (Bool,Int,String) = (true,404,"Not Found")
let (isLoginSuccess,_,_) = connectionResult
if(isLoginSuccess){
println("登录失败!!!")
}
//5.可选型Optionals:或者是一个值,或者是没有值nil
var a:Int?
a = 12
let userInput = "18"
var age = userInput.toInt()
if(age != nil){
println("your age is \(age)");
println("your age is \(age!)");//一定是有值的,安全的,即可选型的解包
println("your age is " + String(age!));
}else{
println("Invalidate userInput");
}
//Optional Binding
if let userAge = userInput.toInt()
{
println("your age is \(userAge)");
}else{
println("Invalidate userInput");
}
//可选型
let strName:String? = "test"
strName
strName!
let strB:String! = "test"
strB
原文链接:https://www.f2er.com/swift/326052.html