最近看了看swift的知识,仅是记录学习过程,如果你是swift小白,可能有一些用呢!😁
下面是swift最基本最简单的一些语法(由于swift目前时时变化,使用语法可能有一定的出入)
1.逻辑判断
<pre>
var sel = 1 > 0
if sel {
print("is 1")
}else {
print("is 0")
}
var score = 101
if score > 100 || score < 0 {
print("不合理");
}else{
print("合理")
}
var l = true ? 1 : 2
func onlinee(age: Int, ID: Bool, maney: Bool) -> () {
guard age > 20 else{
print("条件不成熟");
return;
}
guard ID else{
print("条件不成熟");
return;
}
guard maney else{
print("条件不成熟");
return;
}
print("一起准备OK");
}
</pre>
2.控制流程
<pre>
let ss = 100
// switch 可以不写 break 需要继续时 fallthrough
switch ss {
case 1: print("1")
fallthrough
case 2: print("2")
case 3: print("3")
case 100,200,300: print("111")
fallthrough
default: print("other")
}
// 各种类型比较
switch 1.4 {
case 1.2: print("1.2")
case 1.0: print("1.0")
case 1.2,1.4,1.4434: print("ooo")
fallthrough
default: print("other")
}
// 多个
switch "1" {
case "222","1": print("222")
default: print("other")
}
// 区域比较
switch 1 {
case 1..<10:
print("1..<101..<101..<101..<101..<101..<101..<101..<101..<10")
default:
print("")
}
for index in 0...1 {
print(index)
}
0..<10 // [1 10)
0...10 // [1 10]
for var a = 0; a < 10; a++ {
print(a)
}
for index in 1...2 {
print(index)
}
for index in 1..<2 {
print(index)
}
for _ in 1...9 {
print("hi ")
}
while (false) {
print("1")
}
repeat {
}while(false)
print("学习情况: \(otherWithScr(60.1))")
func otherWithScr(scr: Double) -> (String) {
var string: String
switch scr {
case 0.0..<60.0:
string = "不及格"
fallthrough
case 60.0..<70.0:
string = "及格"
case 70.0..<80.0:
string = "良好"
case 80.0...100.0:
string = "优先"
default:
string = "分数错误"
}
return string
}
</pre>
3.String 与 NSString
<pre>
// MARK: - - String
// 1 创建字符串
let str = "string"
let str111 = String()
print(str)
// 2 遍历字符串
for c in str.characters {
print(c)
}
// 3 字符串拼接
let str1 = "1"
let str2 = "2"
let str3 = str1 + str2
print(str3)
// 4 字符串log 和各种拼接
let name = "ldh"
let age = 20
let height = 199
let info = "name\(name) age\(age) height\(height)"
let min = 20
let second = 4
// 5.格式化
let time = String.init(format: "%.2d : %.2d", min,second)
print("time = \(time)")
// 6.字符串分割
//time.substringToIndex(<#T##index: Index##Index#>)
//time.substringWithRange(<#T##aRange: Range<Index>##Range<Index>#>)
//time.substringFromIndex(<#T##index: Index##Index#>)
let time1 = (time as NSString).substringFromIndex(1) as String
print(time1)
</pre>
4.Array
<pre>
// 1.创建方式
let array = ["1"]
let array1: [String] = ["1"]
let array2: Array = ["1"]
let array3: Array<String> = ["1"]
let array4: Array<String>
array4 = [""]
let array5: Array<String> = Array()
let array6 = Array<String>()
let array7 = String
let array8 = ["1", 1.0, 1]
var muarray = [""]
muarray.append("1")
muarray.append("")
muarray.removeAtIndex(0)
muarray.removeAll()
muarray.removeLast()
muarray.removeFirst()
for name in muarray {}
for i in 0..<2 {}
for name in muarray[0..<2] {}
</pre>
5.Dictionary
<pre>
let dic1 = ["1": ""]
let dic2 = String: String
let dic3 = Dictionary<String, String>()
let dic4 = ["k": "1", "key": 1]
let dic5 = Dictionary<String, NSObject>()
var mdict1 = [String: String]()
var mdict2 = ["key": "a"]
mdict1["k"] = "1"
mdict1.count
var mdict3 = [String: AnyObject]()
mdict1["key11"] = "add"
mdict1["key11"] = "修改"
mdict1.removeValueForKey("key11")
mdict1.removeAll()
let obj = mdict1["key11"]
for key in mdict1.keys {}
for v in mdict1.values {}
for dic in mdict1 {}
for (key, v) in mdict1 {}
let d1 = ["1": "2"]
let d2 = ["2": "kk"]
// 错误 d1+d2 需要遍历的方式合并dict
</pre>
6.元组 (swift特有,oc没有)
<pre>
let info = ("ldh", 100, 2.0)
info.0
info.1
info.2
let info1 = (name: "ldh", age: 100, height: 2.0)
info1.0
info1.name
info1.age
let (name,age) = ("ldh", 100)
name
age
</pre>
7.可选类型 (swift特有,oc没有)
<pre>
// 2.可选类型
// var name1: String = nil 错误
// var name1: Optional<String> = nil;
var name1: String? = nil
// 2 解包 非常危险 可能崩溃
name1 = "1"
print(name1!)
if name1 != nil {
print(name1!)
print(name1!)
print(name1!)
print(name1!)
print(name1!)
}
if let tempName = name1 {
print(tempName)
}
if let name1 = name1 {
print(name1)
}
// 返回的是可选类型(可能为nil的类型)
var url1 = NSURL.init(string: "")
// 自定义了一个 f可选类型
var url2: NSURL
// 会报类型不同的产物 url2 = url1
url2 = url1! // 强制直接解包有安全隐患
if url1 != nil {url2 = url1!} // 判断解包
if let url1 = url1 {url2 = url1} // 优化的判断解包
var url = NSURL.init(string: "")
if let url = url {
var res = NSURLRequest.init(URL: url)
}
func a(){
var obj: AnyObject? = NSObject()
// 解包1
obj!.attributeKeys
// 解包2
if let obj = obj {
obj.attributeKeys
}
// 解包3
obj?.attributeKeys
}
</pre>
网友评论