生活随笔
收集整理的這篇文章主要介紹了
swift5.x基础知识入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
swift5.x基礎知識入門
源碼在我的主頁下,歡迎下載,我非常喜歡swift和oc的語法,兩者相似,我堅信天下沒有學不會的技術。
- 基本介紹
///這里只介紹一些初學swift的基本概念,需要提前了解的東西
/*
1.swift語法和OC不一樣,OC每一行代碼都是以分號";"結尾的,但是swift結尾是沒有結尾標識符的
2.OC只要語法正確就可以,不用整理格式,但是swift非常注重格式,比如 a = 3 ,在=的左右必須都有一個空格,或同時有多個,當=左右空格個數不一樣時,比如 b= 2 這樣會報錯
3.OC調用方式的時候是,是類名或對象名+空格+方法名,然后用中括號括起來:[Person saveCall];
swift是類名或對象名+.+方法名即可: person.saveCall ,類似與OC的點語法
4.swift語法整體兼容OC,可以通過橋接和OC無縫對接
5.swift語法風格整體類似于Java
6.新建的swift類,只有一個.swift文件,沒有.h文件和.m文件
7.swift可以使用cocoapods,方法同OC一樣
8.不需要導入頭文件,可以直接引用其他類里面的方法或屬性
9.swift沒有宏這個概念
10.swift沒有類擴展這個概念
11.沒有main.m文件 @UIApplicationMain是程序入口標識
12.class 標示聲明類 {} 用來標識類的范圍
13.構造函數 [xxx alloc] initWithXXX] ==> xxx(XXX:) ;[alloc init] => ()
14.UIButtonTypeContactAdd => 枚舉類型.枚舉值(簡寫 .枚舉值)
15.在當前類中訪問屬性或者方法的時候 可以省略 self. 推薦不寫, 后面的閉包中必須添加self.
16.按鈕的監聽事件 使用 #selector
import UIKit
class ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()let area1
= getArea(a
: 2, b
: 4)let area2
= getArea2(width
: 3, height
: 4)print(area1
,area2
)numOne()fiveDemo()}func firstDemo1() {let a
= 10print(a
) var b
= 20b
= 30 let c
: Int = 10let d
: CGFloat = 20.3let e
: Float = 3.1415let res2
= c
+ Int(d
)print(res2
)}func scondDemo() {let myView
= UIView(frame
: CGRect(x
: 100, y
: 100, width
: 100, height
: 100))myView
.backgroundColor
= UIColor.green
let btn
= UIButton(type
: UIButton.ButtonType.custom
)myView
.addSubview(btn
)view
.addSubview(myView
)btn
.addTarget(self, action
: #
selector(btnDidClick
), for: .touchUpInside
)}@objc func btnDidClick() {print("我被點擊了")}func thirdDemo() {var a
:Int? = 10a
= nilvar b
: Int? = 15let c
= (b
?? 0) + 20var d
= 10let res
= 20 + d
print(res
,(b
?? 0),c
,d
)}func fourDemo() {let a
= 13if a
< 14 {print("小,很小,非常小")} else {print("大,很大,非常大")}let urlString
= "http://www.baidu.com"let url
= URL(string
: urlString
)if url
!= nil {let req1
= URLRequest(url
: url
!)}if let url2
= URL(string
: urlString
) {let req2
= URLRequest(url
: url2
)}let urlStr
= "http://www.baidu.com"guard var url1
= URL(string
: urlStr
) else {return}let req
= URLRequest(url
: url1
)var age1
: Int? = 15var age2
: Int? = 20if age1
!= nil && age2
== nil {}guard let ag1
= age1
,let ag2
= age2
else {print("嘎嘎嘎")return;}let x
= "20000"switch x
{case "20000","30000":print("架構師")case "18000":print("高級開發工程師")case "12000":print("中級開發工程師")case "6000":print("菜逼")default:print("逗比")}}func fiveDemo() {let range
= 0...10for i
in range
{print(i
)}print("----------------------")for i
in 0...10 {print(i
)}for _ in 0..<10 {print("😑")}let _ = 11}func sixDemo() {let str
: String = "長得帥可以撩妹,長丑是騷擾"let NSstr = str
as NSStringprint(NSstr)let str1
= "長得帥可以撩妹,長丑是騷擾"let len
= str1
.lengthOfBytes(using
: .utf8
)print(len
)let length
= str1
.countprint(length
)for character
in str1
{print(character
)}let str2
= "別低頭,綠帽會掉"let str3
= "別哭泣,老王會笑"let res1
= str2
+ str3
print(res1
)let name
= "王哥"let age
= 13let res2
= name
+ String(age
)let res3
= name
+ "\(age)"let res4
= "\(name)\(age)"print(res2
,res3
,res4
)let z
= 9let x
= 1let h
= 20let res5
= String(format
: "%02d:%02d:%02d", z
,x
,h
)print(res5
)let str4
= "我喜歡手指在鍵盤上飛舞的感覺"let start
= str4
.index(str4
.startIndex
, offsetBy
: 3)let sub1
= str4
.suffix(3)let sub2
= str4
.suffix(from
: start
)print(sub1
,sub2
)}func sevenDemo() {let array
: [Any] = ["阿大,阿二,阿三",18]print(array
)var emptyArray
= [String]()var arr
= ["大哥","二哥","三哥","四哥"]arr
.append("老司機")arr
[0] = "姐姐"arr
.remove(at
: 1)for str
in arr
{print(str
)}for str
in arr
.reversed() {print(str
)}for (index
,value
) in arr
.enumerated().reversed() {print(index
)print(value
)}let array1
: [Any] = ["老張","老李",19]let array2
: [Any] = ["大哥","大姐",23]let array3
= array1
+ array2
print(array3
)let dict
: [String : Any] = ["name" : "玄哥","age" : "21"]var emptyDict
= [String : Any]()var dic
: [String : Any] = ["name" : "玄哥","age" : "21"]dic
["love"] = "鏡子"dic
["love"] = "小鏡子"dic
.removeValue(forKey
: "age")for (key
,value
) in dic
{print(key
,value
)}}func getArea(a
: Int,b
: Int) -> Int {return a
* b
}func getArea2(width a
:Int,height b
: Int) -> Int {return a
* b
}func numOne() {func numTwo(a
: Int,b
: Int) {print(a
* b
)}numTwo(a
: 2, b
: 4)}
}
總結
以上是生活随笔為你收集整理的swift5.x基础知识入门的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。