2016年11月18日 星期五 晚
11月的集宁,相对于来大学的以往两年反倒给人一些暖意,但毕竟是晚上,天气预报的降温也渐渐的若隐若现了,集宁的冬天终于要来了。想想大三正在经历过的集宁,突然觉得有些怀念,暖暖的集宁竟给人一丝可爱的留念的味道,貌似之前寒风刺骨的感觉没有那么的强烈,那么的“可怕”了。每次从教室到食堂再到宿舍的三点一线途中,总有种被集宁的大风吹成“傻狗”的感觉,然而今年,却对她的看法发生了改变。我想,我是爱她的,而她,也是爱我们的。
现在的我正坐在电脑前写着我的swift实训心得。想想,这是大三第一学期第一次实训的第5天,准确来说是第4天,因为第1天的时候,学校和蓝鸥进行了“共建专业开班典礼”,作为蓝鸥派来的老师,锋哥大概的给我们介绍了关于他们公司的一些内容以及现在各个语言的发展情况等,所以实训是从周二才开始的。随着这五天的相处,感觉锋哥讲课比较快,同学们很多有些跟不上,而且原来学的也都忘的差不多了,后来才知道锋哥在他公司是直接带人做项目的,所以光讲重点的部分,自然讲的也快。虽然锋哥讲课快,但说实话其实锋哥是个很牛X的人,上课带我们打代码也很快,讲各个重要的知识点,控件的属性、方法时也都特别仔细,概念大家不懂的时候他也会举通俗的🌰,尽量让大家听懂,还让我们下一些与专业方面相关的APP、网页那些让我们课下自己进行学习,感觉他很尽责,也很努力的让我们多去学一些东西。话不多说,给峰哥个6,希望他能一直陪着我们学习。
这次实训主要是以swift3.0相关的内容展开的,虽然大二的时候对swift课程进行过简单、系统性的学习,但当时我们所学习的是swift2.0的内容,还有一点:大家经过一年的其他科目的学习,对swift的基本语法已经模糊了,更何况是改进过的新内容。所以这次的实训可谓是又从零开始的学习。但相比之前,同学们的积极性也有所提高,每个人都有想学习的欲望,并且都希望可以学会。而且这次的实训较之前的时间较长,完全可以从什么都不会开始学习,课下也有好多时间可以进行练习,对代码进行学习记忆,总之课程的安排相对还是比较合理的。
下面,我就对近几天实训的内容进行总结,其中包括一些重点,非重点,如果内容不够严密、不够好,希望不要喷。
day01
定义常量、变量
//自动类型推断 var 定义变量
var str = "Hello, playground"
//let 定义常量(不能修改的量)
var name = "Hello, plyground"
基本数据类型
- 字符、整型、浮点型、双精度浮点型
//数据类型
//1⃣️字符
let gender : Character = "M"
//2⃣️整型
let age :Int = 12
//3⃣️浮点型
let height : Float = 12.2
//4⃣️双精度浮点型
let height1 : Double = 12.222
- 字符串
//5⃣️字符串
let name1 : String = "Lanou"
//长度
let length = name.characters.count
//拼接
let name2 = name + "pinjie"
var name3 = name + "\(name1)"
//插入
//获取第几个位置
let index = name3.index(name3.startIndex,offsetBy:5)
//插入上面获取的位置
name3.insert(contentsOf: "charu".characters/*字符集合*/, at: index)
name3.insert("a", at: name3.startIndex)/*一个字符*/
//遍历
for item in name3.characters {
}
//切割
var school = "Jiningshifan"
let index1 = school.index(school.startIndex, offsetBy:2)
let newSchool = school.substring(to: index1)
print(newSchool)
- 数组
//6⃣️数组
//array
var arr = [1,2,3]
var arr1 : [Int] = [Int]()
//遍历
for (index, item) in arr.enumerated(){
print(item)
}
//增加元素
arr1.append(2)
//删除元素
let a = arr1.remove(at: 0)
//更新元素(把第二个元素换成22)
arr[1] = 22
print(arr)
//练习题
var names = ["zhang", "li", "wang", "zhao"]
//index 索引 item 获取的值
for(index,item) in names.enumerated(){
print("index = \(index), item = \(item)")
print("index = \\(), item = \(item)")//"\"转义字符
}
for i in 1...10 {
print(i)
}
- 字典
//7⃣️字典
var dic : [Int: Int] = [1:1, 2:2]
var dic1 : [Int : Int] = [Int : Int]() //空字典
//添加 更新
dic[1] = 2 //更新
dic[3] = 3 //添加
//删除
dic.removeValue(forKey: 1)
print(dic)
//遍历
for item in dic.keys {
print(dic[item]!)
}
- Range
//Range
let student = "yang"
let startIndex = student.index(student.startIndex, offsetBy: 2)
let endIndex = student.index(student.endIndex, offsetBy: -2)
let range = startIndex..<endIndex
student.substring(with: range)
冒泡法
//冒泡(排序算法)
var arr = [2,45,16,8,20]
for i in 0..<arr.count - 1 { //循环多少轮
for j in 0..<(arr.count - i - 1) { //比较多少次
//比较换位置
if (arr[j] > arr[j+1]) {
var temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
print(arr)
二叉树的层级遍历
//二叉树的层级遍历
class Node : NSObject {
var data : Int!
var leftChild : Node?
var rightChild : Node?
}
var node1 : Node = Node()
node1.data = 4
var node2 : Node = Node()
node2.data = 5
var node3 : Node = Node()
node3.data = 6
var node4 : Node = Node()
node4.data = 7
var node5 : Node = Node()
node5.data = 2
node5.leftChild = node1
node5.rightChild = node2
var node6 : Node = Node()
node6.data = 3
node6.leftChild = node3
node6.rightChild = node4
//根节点
var node7 = Node()
node7.data = 1
node7.leftChild = node5
node7.rightChild = node6
func readData(node : Node){
var arr = [Node]() //定义数组
arr.append(node) //根节点装进数组
while arr.count > 0 {
let temp = arr[0]
if let _ = temp.leftChild { //可选绑定
arr.append(temp.leftChild!) //左孩子装进数组
}
if let _ = temp.rightChild {
arr.append(temp.rightChild!)
}
//先打印再删除
print(temp.data)
arr.remove(at: 0) //读取完第一个元素的时候删除
}
}
readData(node: node7)
函数
//函数
//1⃣️ 无参数无返回值
func fun1() {
print("无参数无返回值")
}
fun1()
//2⃣️ 无参数有返回值
func fun2() -> Int{
print("无参数有返回值")
return 0
}
//3⃣️ 有参数有返回值
func func3(parm: Int) -> Int {
print("有参数有返回值")
return 0
}
//4⃣️ 有参数无返回值
func func4(parm: Int) {
print("有参数无返回值")
}
//5⃣️ 不定参数
func func5 (parm : Int...) {
var result : Int = 0
for i in parm {
result+=i
}
print(result)
}
//连加
func5(parm: 1,2,3,4)
//inout相当于指针
func func6 (a :inout Int, b :inout Int) {
let temp = a
a = b
b = temp
}
var a = 10
var b = 20
func6(a: &a, b: &b)
print("a = \(a) b = \(b)")
day02
重写父类(并进行初始化)
class Student : NSObject {
var name : String?
var age : Int?
var gender : Bool?
//override重写父类
override init() { //初始化方法
print("创造了一个学生对象")
}
init(age : Int, gender : Bool, name : String) {
super.init()
self.age = age
self.gender = gender
self.name = name
}
//析构函数(放到类里边)
deinit {
print("对象释放")
}
}
var stu : Student = Student(age:13, gender:false, name:"xiaoming")
print("name = \(stu.name) age = \(stu.age) gender = \(stu.gender)")
类与结构体的区别
- 类
//类
class Person : NSObject {
//存储属性
var name : String?
var age : Int?
//计算属性
var fathersAge : Int {
set {
//newValue是默认值
self.age = newValue - 20
}
get {
return self.age! + 20
}
}
//lazy属性
lazy var st : Student = Student()
//函数
//方法调用
func eat() {
print("吃饭")
}
//类名调用(类函数)
class func happy() {
print("happy")
}
//静态不可继承
static func study() {
print("学习")
}
//类属性
static var fatherName : String?
}
var p : Person = Person()
print(p.st)
- 结构体
//结构体
//1.结构体不能继承
//2.结构体是值类型,类是引用类型
struct Dog {
var name : String?
}
var dog : Dog = Dog()
p.name = "zhangsan"
dog.name = "wangcai"
var p1 = p
var dog1 = dog
p1.name = "lisi"
dog1.name = "dahuang"
print("p.name = \(p.name) dog.name = \(dog.name)")
枚举
//枚举
enum Season : String {
case Spring = "春天"
case Summer = "夏天"
case Autumn = "秋天"
case Winter = "冬天"
//case a,b,c
}
print(Season.Autumn.rawValue)
let season = Season.Spring
//遍历
//switch .. case 必须包含所有的情况,否则使用default
switch season {
case .Spring :
print("1")
fallthrough //贯穿(🍵)
case .Summer :
print("2")
case .Autumn :
print("3")
case .Winter :
print("4")
}
let a = 10
switch a {
case 1...20 :
print("包含10")
case 2...30 :
print("包含10")
default:
print("")
}
协议
//协议
//定义一个协议
protocol PersonProtocol {
func study()
}
//可选协议
@objc protocol StudentProtocol {
@objc optional func happy()
var name : String{set get}
}
class Person: NSObject, StudentProtocol, PersonProtocol {
var newName : String?
var name : String {
set{
self.newName = newValue
}
get{
return self.newName!
}
}
func study() {
}
}
var p : Person = Person()
p.study()
p.name = "zhangsan"
print("p.name = \(p.name)")
- 练习题(1)
//练习题
//求图形的面积及周长(用协议)
//面向协议编程(面向对象的一种)
protocol Shape {
func Area() -> Float
func Perimeter() -> Float
}
//三角形
class Triangle : NSObject, Shape {
var height : Float!
var long : Float!
func Area() -> Float {
return height * long / 2.0
}
func Perimeter() -> Float {
return long * 3
}
}
//正方形
class Square : NSObject, Shape {
var length : Float!
func Area() -> Float {
return length * length
}
func Perimeter() -> Float {
return length * 4
}
}
///圆形
class Circle : NSObject, Shape {
var radius : Float!
func Area() -> Float {
return radius * radius * 3.14
}
func Perimeter() -> Float {
return 3.14 * 2 * radius
}
}
var triangle : Triangle = Triangle()
triangle.height = 2.5
triangle.long = 3.0
print("长方形的面积长为:",triangle.Area())
print("长方形的周长为:",triangle.Perimeter())
var square : Square = Square()
square.length = 4.0
print("正方形的面积长为:",square.Area())
print("正方形的周长为:",square.Perimeter())
var circle : Circle = Circle()
circle.radius = 2.25
print("圆形的面积长为:",circle.Area())
print("圆形的周长为:",circle.Perimeter())
- 练习题(2)
protocol GirlProtocol {
func payTheBill()
func happy()
}
//delegate
class Girl : NSObject {
//保存代理值
var delegate : GirlProtocol?
//
func shopBigin() {
if let _ = delegate {
delegate?.happy()
}
}
//购物完毕
func shopOver() {
if let _ = delegate {
self.delegate?.payTheBill()
}
}
}
class Boy : NSObject, GirlProtocol {
func payTheBill() {
//实现协议
print("买单")
}
func happy() {
print("😄")
}
//打游戏
func playGames() {
print("打游戏")
}
}
let boy : Boy = Boy()
let girl : Girl = Girl()
girl.delegate = boy
girl.shopOver()
girl.shopBigin()
- 练习题(3)
//协议
protocol FileProtocol {
//开始
func Start()
//下载过程中
func downProcess()
//结束
func End()
//发生错误
func Error()
}
//文件类
class File : NSObject {
//保存代理值
var delegate1 : FileProtocol?
//文件管理
func fileManage() {
//开始
if let _ = delegate1 {
delegate1?.Start()
}
//下载过程中
if let _ = delegate1 {
delegate1?.downProcess()
}
//结束
if let _ = delegate1 {
delegate1?.End()
}
//发生错误
if let _ = delegate1 {
delegate1?.Error()
}
}
}
//处理文件类
class handleFile : NSObject, FileProtocol {
//把四种状态传过来
//开始
func Start() {
print("开始")
}
//下载过程中
func downProcess() {
print("下载过程中")
}
//结束
func End() {
print("结束")
}
//发生错误
func Error() {
print("发生错误")
}
}
let file : File = File()
let handlefile : handleFile = handleFile()
file.delegate1 = handlefile
file.fileManage()
day03
泛型
//泛型
//打印数字 打印字符串 打印字符
func myPrintNumber(parm:Int) {
print(parm)
}
func myPrintString(parm:String) {
print(parm)
}
func myPrintCharacter(parm:Character) {
print(parm)
}
//泛型只写一个
func myPrint<T:Collection>(parm:T) {
print(parm)
}
let a = [1,2,3]
myPrint(parm: a)
冒泡排序法
func sort(num : Int, num1 : Int) -> Bool {
//if num >num1 true 否则 false
return num > num1 ? true : false
}
//定义一个变量 把sort赋值给变量
let fun : (Int,Int) -> Bool = sort
//使用变量调用函数
let result = fun(1,2)
print(result)
//冒泡排序法
var arr = [1,3,6,9,12]
func sortArr(parm:(Int,Int) -> Bool) {
for i in 0..<arr.count - 1 {
for j in 0..<arr.count - 1 - i {
if (parm(arr[j], arr[j+1])) {
let temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
}
sortArr(parm: sort)
print(arr)
func sort1(num: Int, num1: Int) -> Bool {
return num < num1 ? true : false
}
//调用
sortArr(parm: sort1)
print(arr)
闭包
//闭包
let block = { (a : Int, b : Int) -> Bool in
return a > b ? true : false
}
sortArr(parm: block)
let upSort : (Int, Int) -> Bool = {
return $0 > $1
}
let downSort : (Int, Int) -> Bool = {
return $0 < $1
}
sortArr(parm: upSort)
sortArr(parm: downSort)
print(arr)
day04
UIView
- 在ViewController.swift中super.viewDidLoad()下键入:
//获取当前控制器view,设置背景颜色为红色
//self.view.backgroundColor = UIColor.red
//初始化
let rect = CGRect(x: 30, y: 30, width: 100, height: 200)
let subView : UIView = UIView(frame: rect)
//添加到父视图上
subView.backgroundColor = UIColor.red
self.view.addSubview(subView)
let subView1 = UIView()
subView1.frame = CGRect(x: 140, y: 140, width: 100, height: 100)
subView1.backgroundColor = UIColor.blue
self.view.addSubview(subView1)
let subView2 = UIView()
subView2.frame = CGRect(x: 40, y: 40, width: 30, height: 30)
subView2.backgroundColor = UIColor.green
subView.addSubview(subView2)
//frame 相对于父视图的
//bounds 相对于自身的坐标
print(subView1.bounds)
//center
let subView3 = UIView()
self.view.addSubview(subView3)
subView3.frame = CGRect(origin: self.view.center, size: CGSize(width: 100, height: 100))
subView3.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
//透明度
//subView3.alpha = 0.1 //这种会影响子视图
subView3.backgroundColor = UIColor(colorLiteralRed: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) //三原色修改透明度
let subView4 = UIView(frame: CGRect(x: 10, y: 10, width: 40, height: 40))
subView4.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
subView3.addSubview(subView4)
//tag 使用2000以上的
subView4.tag = 10001 //10000以上
//subView4.isHidden = true //隐藏
let tagView = subView3.viewWithTag(10001)
print("subView4 = \(subView4),Tag = \(tagView)")
//用户交互🌿
//self.view.isUserInteractionEnabled = false
//self.view.isUserInteractionEnabled = true //打开
//superView
print("superView = \(subView4.superview),subView3 = \(subView3)")
//子视图
for item in self.view.subviews {
//从父视图上移除
item.removeFromSuperview()
}
//程序启动
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//初始化window
let frame = UIScreen.main.bounds
self.window = UIWindow(frame: frame)
//初始化控制器
let myVC = MyViewController()
//设置成window的根视图控制器
self.window?.rootViewController = myVC
//把window设置成系统的主window
self.window?.makeKeyAndVisible()
return true
}
- 在ViewController类中键入:
//开始点击
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("点击了当前的控制器")
}
command+n新建一个Cocoa Touch Class,名为:MyViewController,将ViewController改为自己新建的MyViewController;
在AppDelegate.swift中AppDelegate类中键入:
//程序启动
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//初始化window
let frame = UIScreen.main.bounds
self.window = UIWindow(frame: frame)
//初始化控制器
let myVC = MyViewController()
//设置成window的根视图控制器
self.window?.rootViewController = myVC
//把window设置成系统的主window
self.window?.makeKeyAndVisible()
return true
}
- 在MyViewController.swift中viewDidLoad()事件下键入:
self.view.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
- 运行结果:
day01
定义常量、变量
//自动类型推断 var 定义变量
var str = "Hello, playground"
//let 定义常量(不能修改的量)
var name = "Hello, plyground"
基本数据类型
- 字符、整型、浮点型、双精度浮点型
//数据类型
//1⃣️字符
let gender : Character = "M"
//2⃣️整型
let age :Int = 12
//3⃣️浮点型
let height : Float = 12.2
//4⃣️双精度浮点型
let height1 : Double = 12.222
- 字符串
//5⃣️字符串
let name1 : String = "Lanou"
//长度
let length = name.characters.count
//拼接
let name2 = name + "pinjie"
var name3 = name + "\(name1)"
//插入
//获取第几个位置
let index = name3.index(name3.startIndex,offsetBy:5)
//插入上面获取的位置
name3.insert(contentsOf: "charu".characters/*字符集合*/, at: index)
name3.insert("a", at: name3.startIndex)/*一个字符*/
//遍历
for item in name3.characters {
}
//切割
var school = "Jiningshifan"
let index1 = school.index(school.startIndex, offsetBy:2)
let newSchool = school.substring(to: index1)
print(newSchool)
- 数组
//6⃣️数组
//array
var arr = [1,2,3]
var arr1 : [Int] = [Int]()
//遍历
for (index, item) in arr.enumerated(){
print(item)
}
//增加元素
arr1.append(2)
//删除元素
let a = arr1.remove(at: 0)
//更新元素(把第二个元素换成22)
arr[1] = 22
print(arr)
//练习题
var names = ["zhang", "li", "wang", "zhao"]
//index 索引 item 获取的值
for(index,item) in names.enumerated(){
print("index = \(index), item = \(item)")
print("index = \\(), item = \(item)")//"\"转义字符
}
for i in 1...10 {
print(i)
}
- 字典
//7⃣️字典
var dic : [Int: Int] = [1:1, 2:2]
var dic1 : [Int : Int] = [Int : Int]() //空字典
//添加 更新
dic[1] = 2 //更新
dic[3] = 3 //添加
//删除
dic.removeValue(forKey: 1)
print(dic)
//遍历
for item in dic.keys {
print(dic[item]!)
}
- Range
//Range
let student = "yang"
let startIndex = student.index(student.startIndex, offsetBy: 2)
let endIndex = student.index(student.endIndex, offsetBy: -2)
let range = startIndex..<endIndex
student.substring(with: range)
冒泡法
//冒泡(排序算法)
var arr = [2,45,16,8,20]
for i in 0..<arr.count - 1 { //循环多少轮
for j in 0..<(arr.count - i - 1) { //比较多少次
//比较换位置
if (arr[j] > arr[j+1]) {
var temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
print(arr)
二叉树的层级遍历
//二叉树的层级遍历
class Node : NSObject {
var data : Int!
var leftChild : Node?
var rightChild : Node?
}
var node1 : Node = Node()
node1.data = 4
var node2 : Node = Node()
node2.data = 5
var node3 : Node = Node()
node3.data = 6
var node4 : Node = Node()
node4.data = 7
var node5 : Node = Node()
node5.data = 2
node5.leftChild = node1
node5.rightChild = node2
var node6 : Node = Node()
node6.data = 3
node6.leftChild = node3
node6.rightChild = node4
//根节点
var node7 = Node()
node7.data = 1
node7.leftChild = node5
node7.rightChild = node6
func readData(node : Node){
var arr = [Node]() //定义数组
arr.append(node) //根节点装进数组
while arr.count > 0 {
let temp = arr[0]
if let _ = temp.leftChild { //可选绑定
arr.append(temp.leftChild!) //左孩子装进数组
}
if let _ = temp.rightChild {
arr.append(temp.rightChild!)
}
//先打印再删除
print(temp.data)
arr.remove(at: 0) //读取完第一个元素的时候删除
}
}
readData(node: node7)
函数
//函数
//1⃣️ 无参数无返回值
func fun1() {
print("无参数无返回值")
}
fun1()
//2⃣️ 无参数有返回值
func fun2() -> Int{
print("无参数有返回值")
return 0
}
//3⃣️ 有参数有返回值
func func3(parm: Int) -> Int {
print("有参数有返回值")
return 0
}
//4⃣️ 有参数无返回值
func func4(parm: Int) {
print("有参数无返回值")
}
//5⃣️ 不定参数
func func5 (parm : Int...) {
var result : Int = 0
for i in parm {
result+=i
}
print(result)
}
//连加
func5(parm: 1,2,3,4)
//inout相当于指针
func func6 (a :inout Int, b :inout Int) {
let temp = a
a = b
b = temp
}
var a = 10
var b = 20
func6(a: &a, b: &b)
print("a = \(a) b = \(b)")
day02
重写父类(并进行初始化)
class Student : NSObject {
var name : String?
var age : Int?
var gender : Bool?
//override重写父类
override init() { //初始化方法
print("创造了一个学生对象")
}
init(age : Int, gender : Bool, name : String) {
super.init()
self.age = age
self.gender = gender
self.name = name
}
//析构函数(放到类里边)
deinit {
print("对象释放")
}
}
var stu : Student = Student(age:13, gender:false, name:"xiaoming")
print("name = \(stu.name) age = \(stu.age) gender = \(stu.gender)")
类与结构体的区别
- 类
//类
class Person : NSObject {
//存储属性
var name : String?
var age : Int?
//计算属性
var fathersAge : Int {
set {
//newValue是默认值
self.age = newValue - 20
}
get {
return self.age! + 20
}
}
//lazy属性
lazy var st : Student = Student()
//函数
//方法调用
func eat() {
print("吃饭")
}
//类名调用(类函数)
class func happy() {
print("happy")
}
//静态不可继承
static func study() {
print("学习")
}
//类属性
static var fatherName : String?
}
var p : Person = Person()
print(p.st)
- 结构体
//结构体
//1.结构体不能继承
//2.结构体是值类型,类是引用类型
struct Dog {
var name : String?
}
var dog : Dog = Dog()
p.name = "zhangsan"
dog.name = "wangcai"
var p1 = p
var dog1 = dog
p1.name = "lisi"
dog1.name = "dahuang"
print("p.name = \(p.name) dog.name = \(dog.name)")
枚举
//枚举
enum Season : String {
case Spring = "春天"
case Summer = "夏天"
case Autumn = "秋天"
case Winter = "冬天"
//case a,b,c
}
print(Season.Autumn.rawValue)
let season = Season.Spring
//遍历
//switch .. case 必须包含所有的情况,否则使用default
switch season {
case .Spring :
print("1")
fallthrough //贯穿(🍵)
case .Summer :
print("2")
case .Autumn :
print("3")
case .Winter :
print("4")
}
let a = 10
switch a {
case 1...20 :
print("包含10")
case 2...30 :
print("包含10")
default:
print("")
}
协议
//协议
//定义一个协议
protocol PersonProtocol {
func study()
}
//可选协议
@objc protocol StudentProtocol {
@objc optional func happy()
var name : String{set get}
}
class Person: NSObject, StudentProtocol, PersonProtocol {
var newName : String?
var name : String {
set{
self.newName = newValue
}
get{
return self.newName!
}
}
func study() {
}
}
var p : Person = Person()
p.study()
p.name = "zhangsan"
print("p.name = \(p.name)")
- 练习题(1)
//练习题
//求图形的面积及周长(用协议)
//面向协议编程(面向对象的一种)
protocol Shape {
func Area() -> Float
func Perimeter() -> Float
}
//三角形
class Triangle : NSObject, Shape {
var height : Float!
var long : Float!
func Area() -> Float {
return height * long / 2.0
}
func Perimeter() -> Float {
return long * 3
}
}
//正方形
class Square : NSObject, Shape {
var length : Float!
func Area() -> Float {
return length * length
}
func Perimeter() -> Float {
return length * 4
}
}
///圆形
class Circle : NSObject, Shape {
var radius : Float!
func Area() -> Float {
return radius * radius * 3.14
}
func Perimeter() -> Float {
return 3.14 * 2 * radius
}
}
var triangle : Triangle = Triangle()
triangle.height = 2.5
triangle.long = 3.0
print("长方形的面积长为:",triangle.Area())
print("长方形的周长为:",triangle.Perimeter())
var square : Square = Square()
square.length = 4.0
print("正方形的面积长为:",square.Area())
print("正方形的周长为:",square.Perimeter())
var circle : Circle = Circle()
circle.radius = 2.25
print("圆形的面积长为:",circle.Area())
print("圆形的周长为:",circle.Perimeter())
- 练习题(2)
protocol GirlProtocol {
func payTheBill()
func happy()
}
//delegate
class Girl : NSObject {
//保存代理值
var delegate : GirlProtocol?
//
func shopBigin() {
if let _ = delegate {
delegate?.happy()
}
}
//购物完毕
func shopOver() {
if let _ = delegate {
self.delegate?.payTheBill()
}
}
}
class Boy : NSObject, GirlProtocol {
func payTheBill() {
//实现协议
print("买单")
}
func happy() {
print("😄")
}
//打游戏
func playGames() {
print("打游戏")
}
}
let boy : Boy = Boy()
let girl : Girl = Girl()
girl.delegate = boy
girl.shopOver()
girl.shopBigin()
- 练习题(3)
//协议
protocol FileProtocol {
//开始
func Start()
//下载过程中
func downProcess()
//结束
func End()
//发生错误
func Error()
}
//文件类
class File : NSObject {
//保存代理值
var delegate1 : FileProtocol?
//文件管理
func fileManage() {
//开始
if let _ = delegate1 {
delegate1?.Start()
}
//下载过程中
if let _ = delegate1 {
delegate1?.downProcess()
}
//结束
if let _ = delegate1 {
delegate1?.End()
}
//发生错误
if let _ = delegate1 {
delegate1?.Error()
}
}
}
//处理文件类
class handleFile : NSObject, FileProtocol {
//把四种状态传过来
//开始
func Start() {
print("开始")
}
//下载过程中
func downProcess() {
print("下载过程中")
}
//结束
func End() {
print("结束")
}
//发生错误
func Error() {
print("发生错误")
}
}
let file : File = File()
let handlefile : handleFile = handleFile()
file.delegate1 = handlefile
file.fileManage()
day03
泛型
//泛型
//打印数字 打印字符串 打印字符
func myPrintNumber(parm:Int) {
print(parm)
}
func myPrintString(parm:String) {
print(parm)
}
func myPrintCharacter(parm:Character) {
print(parm)
}
//泛型只写一个
func myPrint<T:Collection>(parm:T) {
print(parm)
}
let a = [1,2,3]
myPrint(parm: a)
冒泡排序法
func sort(num : Int, num1 : Int) -> Bool {
//if num >num1 true 否则 false
return num > num1 ? true : false
}
//定义一个变量 把sort赋值给变量
let fun : (Int,Int) -> Bool = sort
//使用变量调用函数
let result = fun(1,2)
print(result)
//冒泡排序法
var arr = [1,3,6,9,12]
func sortArr(parm:(Int,Int) -> Bool) {
for i in 0..<arr.count - 1 {
for j in 0..<arr.count - 1 - i {
if (parm(arr[j], arr[j+1])) {
let temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
}
sortArr(parm: sort)
print(arr)
func sort1(num: Int, num1: Int) -> Bool {
return num < num1 ? true : false
}
//调用
sortArr(parm: sort1)
print(arr)
闭包
//闭包
let block = { (a : Int, b : Int) -> Bool in
return a > b ? true : false
}
sortArr(parm: block)
let upSort : (Int, Int) -> Bool = {
return $0 > $1
}
let downSort : (Int, Int) -> Bool = {
return $0 < $1
}
sortArr(parm: upSort)
sortArr(parm: downSort)
print(arr)
day04
UIView
- 在ViewController.swift中super.viewDidLoad()下键入:
//获取当前控制器view,设置背景颜色为红色
//self.view.backgroundColor = UIColor.red
//初始化
let rect = CGRect(x: 30, y: 30, width: 100, height: 200)
let subView : UIView = UIView(frame: rect)
//添加到父视图上
subView.backgroundColor = UIColor.red
self.view.addSubview(subView)
let subView1 = UIView()
subView1.frame = CGRect(x: 140, y: 140, width: 100, height: 100)
subView1.backgroundColor = UIColor.blue
self.view.addSubview(subView1)
let subView2 = UIView()
subView2.frame = CGRect(x: 40, y: 40, width: 30, height: 30)
subView2.backgroundColor = UIColor.green
subView.addSubview(subView2)
//frame 相对于父视图的
//bounds 相对于自身的坐标
print(subView1.bounds)
//center
let subView3 = UIView()
self.view.addSubview(subView3)
subView3.frame = CGRect(origin: self.view.center, size: CGSize(width: 100, height: 100))
subView3.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
//透明度
//subView3.alpha = 0.1 //这种会影响子视图
subView3.backgroundColor = UIColor(colorLiteralRed: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) //三原色修改透明度
let subView4 = UIView(frame: CGRect(x: 10, y: 10, width: 40, height: 40))
subView4.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
subView3.addSubview(subView4)
//tag 使用2000以上的
subView4.tag = 10001 //10000以上
//subView4.isHidden = true //隐藏
let tagView = subView3.viewWithTag(10001)
print("subView4 = \(subView4),Tag = \(tagView)")
//用户交互🌿
//self.view.isUserInteractionEnabled = false
//self.view.isUserInteractionEnabled = true //打开
//superView
print("superView = \(subView4.superview),subView3 = \(subView3)")
//子视图
for item in self.view.subviews {
//从父视图上移除
item.removeFromSuperview()
}
//程序启动
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//初始化window
let frame = UIScreen.main.bounds
self.window = UIWindow(frame: frame)
//初始化控制器
let myVC = MyViewController()
//设置成window的根视图控制器
self.window?.rootViewController = myVC
//把window设置成系统的主window
self.window?.makeKeyAndVisible()
return true
}
- 在ViewController类中键入:
//开始点击
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("点击了当前的控制器")
}
command+n新建一个Cocoa Touch Class,名为:MyViewController,将ViewController改为自己新建的MyViewController;
在AppDelegate.swift中AppDelegate类中键入:
//程序启动
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//初始化window
let frame = UIScreen.main.bounds
self.window = UIWindow(frame: frame)
//初始化控制器
let myVC = MyViewController()
//设置成window的根视图控制器
self.window?.rootViewController = myVC
//把window设置成系统的主window
self.window?.makeKeyAndVisible()
return true
}
- 在MyViewController.swift中viewDidLoad()事件下键入:
self.view.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
- 运行结果:
(注:文章代码部分属于后加,与前两段写作时间不是同一天。)