实训第一周心得博客
2016-11-20 本文已影响0人
文艺小年青
实训第一周心得
第一天
- FirstDay-Lesson1
- 老师介绍了playground的使用方法,以及各种功能,
- 以下是第一节课所学习的代码
//自动类型推断 var 定义变量
var str = "Hello, playground"
//let 定义常量(不能修改的量)
let name = "Hello,playground"
//数据类型
//字符串
let name1 : String = "Lanou"
//字符
let gender : Character = "M"
//整型
let age :Int = 12
//浮点型
let height : Float = 12.2
//双精度浮点型
let height1 : Double = 12.222
//字符串
//长度
let length = name.characters.count
//拼接
var name3 = name + "pin\(123)jie"
print("name = \(name3)")
let index = name.index
//遍历
for item in name3.characters {
}
//切割
var school = "Jininshifan"
let index1 = school.index(school.startIndex,offsetBy:2)
let newSchool = school.substring(to:index1)
print(newSchool)
//数组
var arr = [1,2,3]
var arr1 : [Int] = [Int]()
//增加元素
arr1.append(2)
//删除元素
let element = arr1.remove(at:0)
//更新元素
arr[1] = 22
print(arr)
//字典
var dic: [Int: Int] = [1:1, 2:2]
// 添加,更新
dic[1] = 2 //更新
dic[3] = 3 //添加
//删除
dic.removeValueForKey(1)
print(dic)
//rang
let student = "yl"
let startIndex = student.index(student.startIndex,offsetBy :2)
let endIndex = student.index(student.endIndex,offsetBy:-2)
let rang = startIndex..<endIndex
student.substring(with:rang)
- Firstday-Lesson2
- 这节课学习了数组的三种遍历方式
- 以及冒泡排序
/*
数组的三种遍历方法
一:
for item in names{
print(item)
}
二:
//index 索引 item 获取的值
for(index,item)in names.enumerated(){
print("index = \(index),item = \(item)")// “ \\ ”转义字符
}
三:
for i in 1...<10{
print(i)
}
*/
//冒泡排序
var arr = [2,8,4,10,31,20]
//冒泡
for i in 0..<arr.count - 1 {//循环多少轮
for j in 0..<(arr.count - i - 1){//比较多少次
//比较换位置
if (arr[j] > arr[j+1]){
let temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
print (arr)
- Firstday-Lesson3
- 第三节课,简单介绍了类的定义,强制拆包(解析)以及可选类型等。
类的定义
/*
class 类名:父类{
}
*/
class Node :NSObject {
// !强制拆包(解析)
var data: Int!
// ?可选类型
var leftChild : Node?
var rightChild :Node?
}
var name: String?
print(name!)
- Firstday-Lesson4
- 第四节课,介绍了一个相当于数列的题型,
- 还有函数的参数及返回值等
- 不定参数
- 声明一个inout 函数,并且将传进来的值进行交换
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 = 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
}
fun2()
//3,有参数有返回值
func fun3() {
print("有参数有返回值")
}
fun3()
//4,有参数无返回值
func fun4() {
print("有参数无返回值")
}
fun4()
//不定参数
func func5 (parm : Int...) {
var result: Int = 0
for i in parm {
result += i
}
print(result)
}
//声明了一个inout 函数 (有两个参数) 并且将传进来的值进行交换
func fun6(a : inout Int, b: inout Int) {
let temp = a
a = b
b = temp
}
var a = 10
var b = 20
fun6(a: &a,b: &b)
print("a = \(a) b = \(b)")
第二天
- SecondDay-Lesson1
- override 重写父类,init 初始化方法
- deinit 析构函数
- 类的存储属性和计算属性
- lazy 属性
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 st : Student = Student(age: 12, gender: false, name: "22")
print("st.name = \(st.name) st.gender = \(st.gender) st.age = \(st.age)")
// 类
class Person : NSObject {
var name : String? // 存储属性
var age : Int? // 存储属性
//计算属性
var fatherAge : 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?
}
//结构体 //1.结构体不能继承 //2,结构体是值类型,类是引用类型
struct Dog {
var name : String?
}
var p : Person = Person()
//print(p.st)
p.name = "张三"
var dog : Dog = Dog()
dog.name = "汪柴"
var p1 = p
var dog1 = dog
p1.name = "李四"
dog1.name = "大黄"
print("p.name = \(p.name) dog.name = \(dog.name)")
- SecondDay-Lesson2
- 这节课学习了协议
- 学习了用协议求出三角形,正方形,长方形的面积,周长。
//定义一个协议
protocol PersonProtocol {
func study()
}
//可选协议
@objc protocol StudentProtocol {
@objc optional func happy()
var name1 : String{set get}
}
class Person : NSObject,PersonProtocol,StudentProtocol{
//internal var name1: String = ""
var newname : String?
var name1: String {
get {
return self.newname!
}
set {
self.newname = newValue
}
}
func study() {
}
}
//三角形
//正方形
//长方形
protocol TuxingProtocol {
func MJ() -> Float
func ZC() -> Float
}
class ThreeAngle:NSObject,TuxingProtocol {
var height : Float!
var d : Float!
func MJ() -> Float {
return height * d / 2.0
}
func ZC() -> Float { //等边三角形
return d * 3;
}
}
class FourAngle: NSObject,TuxingProtocol{
var b : Float!
func MJ() -> Float {
return b * b
}
func ZC() -> Float {
return b * 4
}
}
class zeroAngle:NSObject,TuxingProtocol{
var radio :Float!
func MJ() -> Float {
return radio * radio * 3.14;
}
func ZC() -> Float {
return radio * 2 * 3.14
}
}
var t : ThreeAngle = ThreeAngle()
t.d = 3
t.height = 4
print("三角形的面积是:\(t.MJ())")
print("三角形的周长是:\(t.ZC())")
- SecondDay-Lesson3
- 设置代理,并且学习了男孩女孩购物的题型
- 练习了文件下载:开始,结束,错误等的题型
//文件类
protocol FileProtocol {
func Begin() ;
func Xzz() ;
func End() ;
func Error() ;
}
class File : NSObject {
var delegate : FileProtocol?
override init() {
}
//开始
func FileBegin() {
if let _ = delegate {
delegate?.Begin()
}
}
//下载过程中
func FileXzz() {
if let _ = delegate {
delegate?.Xzz()
}
}
//结束
func FileEnd() {
if let _ = delegate {
delegate?.End()
}
}
//发生错误
func FileError() {
if let _ = delegate {
delegate?.Error()
}
}
}
//处理文件类
class MakeFile : NSObject,FileProtocol {
internal func Error() {
print("下载发生错误")
}
internal func End() {
print("下载结束")
}
internal func Xzz() {
print("文件正在下载")
}
internal func Begin() {
print("下载开始")
}
//把四种状态传递过来
/*func Begin() {
print("下载开始")
}
func Xzz() {
print("文件正在下载")
}
func End() {
print("下载结束")
}
func Error() {
print("下载发生错误")
}*/
}
let file : File = File()
let makefile : MakeFile = MakeFile()
file.delegate = makefile
file.FileBegin()
file.FileEnd()
file.FileXzz()
file.FileError()
- ThirdDay-Lesson1
- 泛型,打印数字,打印字符串,打印字符
- 闭包
- 冒泡排序以及调用
//泛型
//打印数字 打印字符串 打印字符
func MyPrintNumber(parm:Int) {
print(parm)
}
func MyPrintString(parm:String) {
print(parm)
}
func MyPrintCharacter(parm:String) {
print(parm)
}
//使用泛型之后一个就可以了
func MyPrint<T>(parm :T) {
print(parm)
}
//闭包
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 = [10,2,5,2,9,2,5,6]
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)
- ThirdDay-Lesson2
- 这节课进行了小测试
//第一题 对一个数组进行遍历,要求使用两种方式。(数组自定义)
/*
var arr = [2,4,5,7,6,8,1]
/*1,
for name in arr {
print(name)
}
*/2,
for(index, item)in arr.enumerated() {
print("index = \(index), item = \(item)")
}
/*3,
for index in 0..<arr.count {
print(arr[index])
}
*/
*/
//第二题 声明一个函数,要求函数功能要实现对一个数组的排序,并且把这个数组返回。(冒泡排序)
/*
func fun(num : Int,num1 : Int) -> Bool {
return num > num1 ? true : false
}
let fun2 : (Int,Int)->Bool = fun
//let result = fun2[1,2]
var arr = [3,5,14,6,8,43]
func fun2Arr(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
}
}
}
}
fun2Arr(parm: fun)
print(arr)
*/
//第三题 声明一个inout函数,这个函数有两个参数,并且实现交换传递进来的参数值。
/*
func fun(a : inout Int, b : inout Int) {
let temp = a
a = b
b = temp
}
var a = 10
var b = 20
fun(a : &a,b : &b)
print("a = \(a) b = \(b)")
*/
/*
第四题 实现图书管理系统:(60分)
1、使用数组管理所有书籍。
2、数组可以添加、删除。
3、可以从数组根据书名查找书籍,并修改书籍的价格。
4、展示所有书籍清单(书名)。
提示:
(1)声明一个书类: Book
给这个类添加: 书名,价格的属性
(2)声明一个管理书籍类: BookManger
给这个类添加:添加,删除,修改书籍价格,展示所有书籍的方法
*/
struct Book {
var name : String?
var price : Float?
init(name : String, price : Float) {
self.name = name
self.price = price
}
}
class BookMgr : NSObject {
lazy var BookArr : [Book] = [Book]()
func addBook(book : Book) {
self.BookArr.append(book)
}
func deleteBook(name : String) -> String {
//查找这本书
var isExit = false //不存在的
for (index, item) in BookArr.enumerated() {
if item.name == name {
self.BookArr.remove(at: index)
isExit = true
}
}
return isExit ? "删除成功" : "删除失败"
}
func updatePrice(price:Float,withName Name:String) -> String {
var isExit = false
//先查找图书
for(index,item)in BookArr.enumerated() {
if item.name == Name {
//修改价格
BookArr[index].price = price
isExit = true
}
}
return isExit ? "修改成功" : "修改失败"
}
//展示所有的书籍的清单
func showAllName() {
for item in BookArr{
if let _ = item.name{
print(item.name! + "价钱为\(item.price)")
}
}
}
}
let book = Book(name:"zhangsan",price:10.1)
let book2 = Book(name:"zhangsan",price:10.2)
let book3 = Book(name:"zhangsan",price:10.3)
let book4 = Book(name:"zhangsan",price:10.4)
let mgr = BookMgr()
mgr.addBook(book: book)
mgr.addBook(book: book2)
mgr.addBook(book: book3)
mgr.addBook(book: book4)
print(mgr.deleteBook(name: "zhangsan"))
print(mgr.updatePrice(price: 2, withName: "lisi"))
mgr.showAllName()
- ForthDay-Lesson1
- 今天学习了UI,学习了UIView的很多属性,
//获取当前控制器的view,设置背景颜色为红色
//self.view.backgroundColor = UIColor.red
//iPhone6视图 :x = 375 y = 667
//设置坐标大小
/*let rect = CGRect(x: 30, y: 30, width: 100, height: 100)
let subview : UIView = UIView(frame: rect)
subview.backgroundColor = UIColor.green
//添加到父视图上
self.view.addSubview(subview)
let subView1 = UIView()
subView1.frame = CGRect(x: 140, y: 240, width: 100, height: 100)
subView1.backgroundColor = UIColor.yellow
self.view.addSubview(subView1)
//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.8078431487, green: 0.02745098062, blue: 0.3333333433, 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.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
subview3.addSubview(subview4)
//tag 使用2000以上的
subview4.tag = 10001
//subview4.isHidden = true //隐藏
let TagView = subview3.viewWithTag(10001)
print("subview4 = \(subview4), TagView = \(TagView)")
//用户交互
//self.view.isUserInteractionEnabled = false
// superView 父视图
print("superView = \(subview4.superview),subview3 = \(subview3)")
//子视图
for item in self.view.subviews {
//从父视图上移除
item.removeFromSuperview()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("点击了当前控制器")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
总结就先到此为止,不断学习不断记录,
希望付出会有回报
最后,感谢蓝鸥杨少锋老师的不吝传授!
敬礼··