Swift小项目计划学习笔记-Day1

  //  ViewController.swift
  //  000-test
  //
  //  Created by 张云翥 on 2018/7/13.
  //  Copyright © 2018年 张云翥. All rights reserved.
  //

    import UIKit
  //siwft里面不存在基本数据类型,都是结构体
class ViewController: UIViewController {
    //0.快捷键 command + option + /
    //MARK: -视图生命周期
override func viewDidLoad() {
    super.viewDidLoad()  
  //        demo()
  //        demo0()
  //        demo1()
  //        demo3()
  //        demo4(name: "Topus", age: 10)
  //        demo5()
   //        demo6()
  //        demo7()
  //        demo8()
  //        demo9()
}
 //1.Int()是结构体的构造函数
func demo() {
    let x = 10
    let y = 10.5
    print(x + Int(y))
}
//2.color + 回车色块
func demo0()  {
    let v = UIView(frame: CGRect(x: 0, y: 20, width: 200, height: 200))
    v.backgroundColor = #colorLiteral(red: 0.9670192599, green: 0.5405859351, blue: 0.4974349141, alpha: 1)  //3.FIXME: 应该
    
    view.addSubview(v) //4.TODO: 嘿嘿
}

//5.解包的时候发现nil值出错Unexpectedly found nil while unwrapping an Optional value
func demo1(){
    //定义一个可选的Int类型,可能有值也可能有Int型的值
    let x:Int? = 40
    print(x!)//强行解包
    
}

//6.简单的三目运算 有值就是值 没值就是??后面的
//??优先级低  最后做
func demo3(){
    let x:Int? = nil
    let y:Int? = 8
    print((x ?? 0) + (y ?? 0))
    
}


//7.guard let else
func demo4(name:String?,age:Int?) {
    guard let name = name, let age = age else {
        print("姓名或年龄为空")
        return
    }
        print(name + String(age))
    
}

//8.反遍历
func demo5() {
    let r = (0..<5).reversed()
    print(r)
    
}

//9.子串
func demo6()  {
    let str = "我是张云翥"
    
    guard let range = str.range(of: "张云翥") else {
        print("字符串为空")
        return
    }
    
    print(range)
    
  //print(str.substring(with: range)) //弃用
    
}

//10.同时遍历数组和下标
func demo7() {
    let arry = ["zyz","yyz","yb"]
    
    for e in arry.enumerated() {
        print(e)
        //元祖
        print("\(e.offset)\(e.element)")
    }
}

//11.清除数组内容但是保留空间
func demo8()  {
    var arry = ["zyz","yyz","yb"]
    
    arry.removeAll(keepingCapacity: true)
    print("\(arry)\(arry.capacity)")
}

//12.两个类型不一致的数组合并(转Any)
func demo9() {
    var arry1:[Any] =  ["zyz","yyz","yb"]
    let arry2:[Any] = ["zyz","hfus",18]
    arry1 += arry2
    print(arry1)
  }
}

你可能感兴趣的:(Swift小项目计划学习笔记-Day1)