Swift与OC区别
语句结束不需要写“;”换行即可(建议书写)
int,float等类型变成了Int,Float首字母大写了
Swift中新增与NString 用法基本一致的String
Swift中不在有“[""]”一般都是“.”调用
//定义标识符
//let(常量)/var(变量) 名称 :类型 = 初始值
//定义常量
let count : Int = 10;
//定义变量
var value : Float = 10.1;
value = 11;//value的类型还是Float
//建议初始化都优先使用let,如果是var类型定义为let会有提示
//Swift中创建的标识符如果不指定类型,会默认指定类型
//打印:
print(value)
//Swift中没有隐式转化,不会将整形自动转成浮点型
let C = 23
let F = 32.23
//let CF = C + F 报错
let CFOne = Double(C) + F
let CFTwo = C + Int(F)
//定义UI控件
let viewOne : UIView = UIView()
let viewTwo = UIView()
viewOne.backgroundColor = UIColor.red
// if的使用
// 1> if后面的()可以省略
// 2> 判断句不再有非0即真.必须有明确的Bool值:true/false
// 3> 三目运算符没有改变
let a = 10
if a > 10 {
print("a大于10")
}else if (a < 10){
print("a小于10")
}else{
print("a等于10")
}
//guard语句 与 if语句 正好相反 guard语句是false执行 guard语句必须写在方法里面
func betterA (str : String){
guard a > 10 else {
print("a大于10")
return
}
}
//Switch的使用
//let sex = 1.0
let sex = "男"
//同if一样switch后面可以不加()
//case后面不需要break 如果希望case出现穿透,在case语句后加fallthrough
//case后可以判断 Float类型 字符串类型 判断区间
switch sex {
case "男":
print("男");
fallthrough
case "女":
print("女");
default:
print("其他");
}
let score = 100
switch score {
case 0..<60:
print("不及格")
case 60..<80:
print("及格")
case 80..<90:
print("良好")
case 90...100:
print("优秀")
default:
print("不应存在的分数")
}
//for循环没有改变 while循环没有改变
//do-while循环 变为 repeat-while循环
var b = 0
repeat{
b += 1
print(b)
} while b < 5
import UIKit
//--------------------------------字符串的使用--------------------------------
//1.定义字符串
let str = "CF hello world"
//2.遍历字符串中字符
for s in str.characters
{
print(s)
}
//3.字符串拼接
let strOne = "CF"
let strTwo = "52 Swift"
let strCount = strOne + strTwo
print(strCount)
let age = 18
let name = "CF"
//拼接其他标识符的格式: \(标识符的名称)
let people = "\(age) name"
print(people)
//格式化拼接字符串
let min = 3
let second = 18
let data = String(format: "%02d:%02d", min,second)
print(data)
//4.字符串的截取
let urlStr = "www.baidu.com"
let header = (urlStr as NSString).substring(to: 3)
let middle = (urlStr as NSString).substring(with: NSMakeRange(4, 5))
let footer = (urlStr as NSString).substring(from: 10)
//--------------------------------数组的使用--------------------------------
//不可变数组定义(let) 可变数组定义(var)
let array = ["one","two","three","4"]
var mutArray = Array<Any>()
//增加元素
mutArray.append("ONE")
mutArray.append("TWO")
mutArray.append("3")
//删除元素
mutArray.remove(at: 1)
//修改元素
mutArray[1] = "THREE"
//查询元素
mutArray[0]
//数组合并
let arrayOne = ["1","2","3"]
let arrayTwo = ["a","b","c"]
let arrayThree = arrayOne + arrayTwo
//遍历数组
for i in 0..<arrayThree.count {
print(arrayThree[i])
}
for value in arrayThree {
print(value)
}
//--------------------------------字典的使用--------------------------------
// 注意:在swift中无论是数组还是字典都是使用[],但是如果[]中存放的是元素,编译器会认为是一个数组.如果[]中存放的是键值对,编译器会认为是一个字典
//不可变字典定义(let) 可变字典定义(var)
let dict = ["name":"CF","age":"20","height":"1.80"]
var mutDict = Dictionary<String, Any>()
//增加元素
mutDict["name"] = "CF"
mutDict["age"] = "20"
mutDict["height"] = "1.80"
//删除元素
mutDict.removeValue(forKey: "age")
//修改元素
mutDict["height"] = 1.78
//查找元素
mutDict["name"]
//字典遍历
//遍历所有的key
for key in mutDict.keys {
print(key)
}
//遍历所有的value
for value in mutDict.values {
print(value)
}
//遍历所有的key和value
for (key ,value) in mutDict {
print(key)
print(value)
}
//字典的合并只能遍历添加 不能直接“+”
//--------------------------------元组的使用--------------------------------
//Swift中新增数据类型元组,实际上就是数组、字典中,存放不同的类型元素
let userArray = ["CF", 18, 1,88] as [Any]
userArray[0]
let userDict = ["name" : "CF", "age" : 18, "height" : 1.88] as [String : Any]
userDict["name"]
// 元组的基本写法
let userInfo = ("CF", 18, 1.88)
userInfo.0
userInfo.1
userInfo.2
// 给每一个元素起别名
let userInfo1 = (name : "CF", age : 18, height : 1.88)
userInfo1.name
userInfo1.age
userInfo1.height
// 别名就是变量的名称
let (userName, userAge, userHeight) = ("CF", 18, 1.88)
userName
userAge
userHeight
//--------------------------------可选类型的使用--------------------------------
// ? 或者 ! 说明是可选类型
// ?表示为可选类型 !表示为强制解包(如果可选类型无值,强制解包会崩溃)
// 在初始化元素的时候一定要有值 或者?
let myName : String?
myName = "CF"
print(myName as Any) //Optional("CF")
//print(myName!) //CF 强制解包
if myName != nil {
print("方法一:\(myName!)")
}
if let myName = myName {
print("方法二:\(myName)")
}
let urlString = "www.baidu.com"
let url = NSURL(string: urlString)
//1.普通写法
if url != nil {
let request = NSURLRequest(url: url as! URL)
}
//2.可选绑定
if let url = url {
let request = NSURLRequest(url:url as URL)
}
//3.可选绑定的简单写法
if let url = NSURL(string: urlString){
let request = NSURLRequest(url: url as URL)
}
//--------------------------------函数的使用--------------------------------
//1.没有参数没有返回值
func oneFunc() {
print("iPhone")
}
oneFunc()
//2.有参数没有返回值
func valueFunc(value : String) {
print("打印传进来的值:\(value)");
}
valueFunc(value: "哈哈")
//3.没有参数有返回值
func iMessage() -> String {
return "吃了吗?"
}
print(iMessage())
//4.有参数有返回值
func sum(numOne : Int,numTwo : Int) -> Int{
return numOne + numTwo
}
print(sum(numOne: 23, numTwo: 32))
//函数带有默认参数
func add(numOne : Int = 11, numTwo : Int = 22) -> Int {
return numOne + numTwo
}
print(add(numOne: 22,numTwo:33))