Swift iOS/Mac原生开发语言
简介:苹果官方开发语言,iPhone、iPad、Mac、Watch全平台应用开发
小白入门案例1:控制台Hello输出
let site = "doc.yiliancai.com"
print("欢迎学习Swift iOS开发 | \(site)")
小白入门案例2:循环求和1~100
var sum = 0
for i in 1...100 {
sum += i
}
print("总和:\(sum)")
基础实操案例3:用户结构体Model
struct User {
let name: String
let age: Int
}
let user = User(name: "罗秀萍", age: 28)
print(user.name)
基础实操案例4:数组高阶过滤
let list = [User(name:"张三",age:22),User(name:"李四",age:26)]
let adult = list.filter{$0.age > 24}
print(adult.count)
进阶项目案例5:UIKit页面基础布局
import UIKit
class MainVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame:CGRect(x:50,y:100,w:200,h:40))
label.text = "亿联财Swift教程"
view.addSubview(label)
}
}
进阶项目案例6:Alamofire网络请求
import Alamofire
func fetch(){
AF.request("https://static.yiliancai.com").responseString{ res in
print(res.result.success?.count)
}
}
进阶项目案例7:SwiftUI声明式界面
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("SwiftUI学习")
Button("查看教程"){}
}
}
}
企业精通案例8:UserDefaults本地持久化
UserDefaults.standard.set("luoxiuping", forKey: "name")
let name = UserDefaults.standard.string(forKey: "name")
print(name!)
企业精通案例9:URLSession异步下载图片
func download(urlStr:String) async throws -> Data {
guard let url = URL(string:urlStr) else {throw NSError(domain:"url错误",code:-1)}
let (data,_) = try await URLSession.shared.data(from:url)
return data
}
企业精通案例10:APNs推送通知注册
import UserNotifications
class PushMgr: NSObject, UNUserNotificationCenterDelegate {
func requestAuth(){
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.alert,.sound]){ grant,_ in
if grant {UIApplication.shared.registerForRemoteNotifications()}
}
}
}