This Learn Swift series is designed for beginners with NO programming experience. It uses the latest version of Swift 3 and Xcode 8.
1.swift的源文件扩展名.swift
2.不需要编写main函数,程序从上往下执行;不需要每条语句都加分号,同一行有多条代码语句是,中间必须有分号。
3.注释: / 单行注释 /* 多行注释 */ 嵌套注释 : /* /* 注释 */ */
4.常量和变量 let声明常量 var声明变量
vara =1
varb =2
str="wangchao"
letf =10
str=String(29)
varc:Float=2.3
vard:Float=13.90
print(Int(c))
print(Int(d))
print(round(d))
print(round(c))
print(str)
print(a+b)
print(str)
vara =1
varb =2
str="wangchao"
letf =10
str=String(29)
varc:Float=2.3
vard:Float=13.90
print(Int(c))
print(Int(d))
print(round(d))
print(round(c))
print(str)
print(a+b)
print(str)
vara =1
varb =2
str="wangchao"
letf =10
str=String(29)
varc:Float=2.3
vard:Float=13.90
print(Int(c))
print(Int(d))
print(round(d))
print(round(c))
print(str)
print(a+b)
print(str)
vara =1
varb =2
str="wangchao"
letf =10
str=String(29)
varc:Float=2.3
vard:Float=13.90
print(Int(c))
print(Int(d))
print(round(d))
print(round(c))
print(str)
print(a+b)
var str = "Hello, playground"
var swiftString = "Swift is fun"
var hours = 24
var PI = 3.14
var me = ("Mars",26, "wangchao@163.com")
me.0
me.1
me.2
定义常量,并尝试修改报错
let minute = 30
//minute = 20
let不可变,要转换成var类型
Swift根据我们赋值自动判断变量类型 type inference,我们也可以自己指定类型
var x: Int
var s: String //指定类型 type annotation
5.创建一个button
let btn = UIButton()
btn.frame = CGRect(x:100,y:100,width:100,height:100)
btn.backgroundColor = UIColor.red
self.view.addSubview(btn)
6.整数和浮点数
/* 一般使用Int就行,除非有特殊情况 如下两种情况输出相同,因为编译器对Int进行了正确的类型选择*/
Int.min
Int.max
Int64.min
Int64.max
//十六进制 八进制 二进制 十进制
let fifteenInDecimal = 15
let fifteenInHex = 0xf
let fifteenInOctal = 0o17
let fifteenInBinary = 0b1111
let million = 1_000_000 //千分位
//浮点数
var float1:Float = 1/3
var double1:Double = 1/3
//科学计数法
var pi = 31415926e-7