第一节:变量,常量,类型推断,字符,字符串
//swift学习第一节
/*
变量
常量
类型推断
字符
字符串
*/ import UIKit //变量
var str = "swift" //类型推断
str = "hello"
var str1:NSString = "shaoting"
var num =
var num2:Int = //常量
let num3 = //类型推断
//num3 = 123 error:常量不可以修改
let str2:NSString = "pheromone" //Int 有符号的整形
//UInt 无符号的整形 Int.max
Int.min var numFloat : Float = /
var numDouble : Double = / var num4 =
num4.dynamicType //类型
var num5 = 0.14
num5.dynamicType num5 = + 0.14
num5.dynamicType
//num5 = num4 + num5 eroor:类型不同不能相加
num5 = Double(num4) + num5 //初始化字符串,默认值为nil
let strr = ""
let strrr = String() let swift:Character = "S"
let swiftInChar:[Character] = ["s","w","i","f","t"];
let swift1 = String(swiftInChar)
let swift11 = swiftInChar var message = "i love you"
let meessageLength = message.characters.count //message[0] error:swift中,访问字符串中的某个字符不能使用字符串数组的某个下标
message.startIndex // 第一个下标
message.endIndex //最后一个下标
message[message.startIndex.successor()] //第一个下标的下一个值
message[message.endIndex.predecessor()] //最后一个下标的上一个值
message[message.startIndex.advancedBy()] message.insertContentsOf("iOS".characters, at: message.startIndex)//在某个字符串的某个位置加入某些字符
message.removeAtIndex(message.endIndex.predecessor())
//message.removeRange(<#T##subRange: Range<Index>##Range<Index>#>) 删除范围内的内容 //比较字符串
let message1 = "ios hello"
if message != message1{
print("!=")
} let swi = "我是一只猿"
swi.hasPrefix("我")//判断前端是否包含
swi.hasSuffix("猿")//判断后端是否包含 for code in swi.utf8 {
print("\(code)")
} //数组
var strS:[String] = ["ios","swift"];
//var strS1:[] = [12,34]; 没有指定元素类型
第二节:元组Tuple 操作符operators
/*
swift学习第二节
元组 Tuple
操作符 Operators */ import UIKit //元组 :自己定义一组数据,类型也自己定义
let success = (,"HTTP OK")
let fileNotFound = (,"File not found")
let me = (name:"shaoting",no:,email:"510738319@qq.com")
success.
success.
me.name var (code,message) = success
print(code)
print(message) //操作符 Operators
var a =
var b = let sum = a + b
let sub = a - b
let mul = a * b
let div = a / b
let mod = a % b
let mod1 = % b += // b = b+10
b -= // b = b-10
b *= // b = b*10
b /= // b = b/10
b %= // b = b%10 let isEqual = sum ==
isEqual ? "YES" : "NO" //区间运算符
//包含1到5
for index in ...{
print(index) // 1 2 3 4 5
} for index in ..<{
print(index) // 1 2 3 4
}
第三节:集合--有序集合(数组)无序集合(set,dictionary)
/*
swift学习第三节
集合(数组,set,dictionary)
有序集合: 数组
无序集合: set dictionary
*/ import UIKit
//数组
var ary1:Array<Int> = Array<Int>()//定义一个值为空的int类型的数组
var ary2:[Int] = ary1
var ary3 = ary1 var three = [Int](count: , repeatedValue: )
var six = three + three //相同类型的数组可以做运算 var four = [,,,]
four.count if ary1.isEmpty{
print("ary1 为空数组")
} ary1.append() // 添加元素
ary1 += [,,] // 添加多个
ary1[...]
ary1[..<]
ary1[...] = [,] //修改元素
ary1
ary1.insert(, atIndex: ) //在某个下标添加元素
ary1.removeAtIndex() //删除某下标的元素
ary1.removeFirst() //删除第一个元素
ary1.removeLast() //删除最后一个元素
ary1
for (index,value) in ary1.enumerate(){
print("index : \(index) value : \(value)")
} //set
//输出hash值
let number =
number.hashValue
let PI = 3.14
PI.hashValue
let string = "Swift"
string.hashValue //定义一个元素类型为字符的空set
let emptySet = Set<Character>()
let vomel:Set<Character> = ["a","e","i","o","u"];
var evenSet:Set = [,,,,] //类型推断
evenSet.count //该set的个数
evenSet.removeFirst() //移除set的第一个元素
evenSet.insert() //插入一个元素25
evenSet.contains() //判断是否包含
//因为set是无序集合,想要保证一定顺序可以使用sort
for number in evenSet.sort(){
print(number)
} //setA 和 setB 的交集
var setA : Set = [,,,,]
var setB : Set = [,,,]
let setiAB : Set = setA.intersect(setB)
//setA 和 setB 的对称差
let seteAB : Set = setA.exclusiveOr(setB)
//setA 和 setB 的并集
let unionAB : Set = setA.union(setB)
//setA对于setB的补集
let subAB : Set = setA.subtract(setB) //判断集合是否相等
if setA == setB{
print("相等")
}else{
print("不相等")
} setB.isSupersetOf(setA) //判断B是否是A的超集
setB.isSubsetOf(setA)//判断B是否是A的子集 //字典
//创建一个key是int value是string的字典
var int2String = [Int:String]()
int2String = [:]
int2String[] = "Ten"
int2String[] = "Twenty" var capitalNumber = [
:"一",
:"二",
:"三"
] capitalNumber[] // 字典某个下标值
capitalNumber.count //字典的个数
capitalNumber.isEmpty //判断字典是否为空
capitalNumber.updateValue("yi", forKey: ) //修改字典某个key对应的值
capitalNumber[] = nil //对某个下标置空
capitalNumber.removeValueForKey() //移除某个key对应的值
capitalNumber
capitalNumber.dynamicType //类型 for (key,value) in capitalNumber{
print("\(key):\(value)")
}
let keyAry = [Int](capitalNumber.keys);
let valueAry = [String](capitalNumber.values);
第四节:循环语句(for while repeat-while switch)
/*
swift学习第四节
循环语句
for while repeat-while switch */
import UIKit for var i = ;i < ; i++ {
print(i)
} let vomel = ["a","e","i","o","u"]
for char in vomel{
print(char)
} for number in ...{
print(number)
} for _ in ...{
print("*",terminator:"")
} var i =
while i < {
print(i)
++i
} //var n = 0
//repeat {
// print(vomel[n])
// ++i
//}while n < 5 let me = "a"
switch me{
case "a":
print("me is \(me)")
case "e":
print("me is \(me)")
case "i":
print("me is \(me)")
case "o":
print("me is \(me)")
case "y":
print("me is \(me)")
default:
print("都不是")
} switch me{
case "a","e","i","o","u":
print("这个是元音")
default:
print("这个不是元音")
} let number =
switch number{
case ...:
print("这个数在1-10之间")
case ...:
print("这个数在11-99之间")
default:
print("这个数大于100")
} let point = (,)
switch point{
case (,):
print("在原点")
case (_,):
print("在x轴上")
case (,_):
print("在y轴上")
case (-...,-...):
print("在该范围内")
default:
print("位置跑偏")
} switch point{
case (let x,):
print("\(x)")
case (,let y):
print("\(y)")
case let(x,y):
print("\(x),\(y)")
} switch point{
case let(x,y) where x == y:
print("X=Y")
case let(x,y) where x == -y:
print("x=-y")
case let(x,y):
print("\(x),\(y)") }
源码下载地址: