IOS学习随笔

时间:2020-11-18 13:29:04   收藏:0   阅读:33

IOS ICON 制作网站

图标生成 https://www.canva.com/

生成整套图标 https://appicon.co


swift驼峰法命名

? 例:diceImageView

? btnRoll


变量申明

字符串差值
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之间的六个数字

摇骰子示例

//
//  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的使用

MVC模式


面向对象编程

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)
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!