IOS學習隨筆
阿新 • • 發佈:2020-11-13
IOS ICON 製作網站
生成整套圖示 https://appicon.co
swift駝峰法命名
- 變數:小駝峰法f命名
例:diceImageView
btnRoll
-
檔名:大駝峰法命名
全大寫
變數申明
-
申明一個變數就是製造一個容器,將資料放進去
- 容器有兩種形式:var 和 let
- var是變數,可變的
- let是常量,是不可變的
let耗記憶體小,var可變。
首先考慮用let,如果提示不能給常量重新賦值,改為var
- 容器有兩種形式:var 和 let
-
不同型別的資料不能直接進行運算(Float和Double不可運算)
字串差值
let fullName = "\(name) liu"
布林型別
進行邏輯判斷時用
let aa = true
let bb: Bool = true
// 預設是Double型別,小數優先使用Double型別
let bb = 3.14
// swift型別推斷:根據等號右側的值推斷是字串
var str = "Hello,playground"
// 在變數中三指輕點提示推斷型別
//申明index1的型別是Int,預設是0,=前後有空格,空格不對稱會報錯 var index1: Int = 0 Int 3 Float 3.14 // 精度不同, Double 3.1415926 // 用的較多 Bool true,false String "Angela","Philipp" // 一定要用雙引號
Int(取隨機數)
index1 = Int.random(in:1...6 ) // 閉區間...會生成1到6之間的六個數字
搖骰子示例
- motionEnded 手機搖晃結束後,執行{}內的程式碼
// // ViewController.swift // Dice // // Created by 徐斌 on 2020/11/10. // import UIKit class ViewController: UIViewController { let diceImages = ["dice1","dice2","dice3","dice4","dice5","dice6"] var index = 0 @IBOutlet weak var diceImageFirst: UIImageView! @IBOutlet weak var sourceLabel: UILabel! @IBOutlet weak var diceImageSecond: UIImageView! @IBAction func btnRoll(_ sender: Any) { rollBegin() } func rollBegin() { let firstIndex = indexRandom() let secondIndex = indexRandom() let sourceNum = firstIndex + secondIndex + 2 diceImageFirst.image = UIImage(named: diceImages[firstIndex]) diceImageSecond.image = UIImage(named: diceImages[secondIndex]) sourceLabel.text = String(sourceNum) } // 搖手機手勢結束之後執行函式 override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { print("搖晃了手機") rollBegin() } func indexRandom() -> Int { return Int.random(in: 0...(diceImages.count-1)) } override func viewDidLoad() { super.viewDidLoad() rollBegin() // Do any additional setup after loading the view. } }
// round 是小數, pow()是平方
let bmi = round(weight / (pow(height,2)))
var sum = 0
// 對1到10進行取模運算,輸出滿足條件的數
for i in 1...10 where i % 2 == 0{
print(i)
}
//外部引數,內部引數 howMany是外部引數,total是內部引數
//一般我們不用外部引數
//也可以寫下劃線 _ ,也不常用
//func song( _ total:Int){
func song(howMany total:Int){
for i in (1...total).reversed(){
print("現在有\(i)"部iphone,賣出了一部,還剩\(i-1)部)
}
print("全部賣光啦")
}
song(howMany: 20)
關於tag的使用
-
使用場景:
設定button的tag值,該值為Int型別,區分哪一個button
-
例子:木琴app
// // ViewController.swift // 木琴 // // Created by 徐斌 on 2020/11/10. // import UIKit //A 是音訊 V 是視訊 Foundation 基礎功能 import AVFoundation class ViewController: UIViewController { var player:AVAudioPlayer! // 檔案需要拖拽到xcode根目錄中,不能拖到訪達,會找不到檔案 let sounds = ["note1","note2","note3","note4","note5","note6","note7"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } // 拖拽UIButton的時候要選擇Action ,type選擇UIButton // button的tag屬性在view那一欄 @IBAction func btnMusic(_ sender: UIButton) { play(tag: sender.tag) } func play(tag:Int) { // Bundle是app的根目錄,main是主目錄,forResource 是資源名字 withExtension是副檔名 let url = Bundle.main.url(forResource: sounds[tag], withExtension: "wav") //列印的是主目錄的絕對路徑 print(Bundle.main.bundlePath) // 在執行播放的時候會提示可能拋錯,需要做異常處理 //錯誤提示:Errors thrown from here are not handled //進行docatch //錯誤提示:Value of optional type 'URL?' must be unwrapped to a value of type 'URL' // 翻譯:可選型別“URL”的值?‘ 必須解包到“URL”型別的值‘ // 提示需要解包,用感嘆號對 url 進行解包 do { // 錯誤提示:Result of 'AVAudioPlayer' initializer is unused // 提示'AVAudioPlayer未被使用,賦值給一個變數 player = try AVAudioPlayer(contentsOf: url!) player.play() }catch{ print(error) } } }
作用域(scope範圍)
- 區域性變數:在{}內定義的變數或常量,在它處是不能用的
- 全域性變數:在{}外定義的變數或常量,在公共區域,都可以呼叫
通俗理解:在{}內定義的變數,其它地方是用不了的
MVC模式
- MVC是一個約定俗成的套路
- Model 資料管理
- View 展示
- Controller 處理資料,傳遞
面向物件程式設計
Object-oriented programming (OOP)
class Question{
let answer:Bool
let questionText:String
init(correctQuestion:String,text:Bool) {
questionText = correctQuestion
answer = text
}
}
// 初始化:從類變成物件的一個過程
let question1 = Question( correctQuestion:"馬雲是中國首富" ,text: true)