大三上半年实训
第一周实训基础知识,以及UIview控件的使用
- 基础知识
- 1.自动类型推断
//自动类型推断 var定义变量
var str = "Hello, playground"
// 定义一个常量(不能修改的量)
let name = "Hello, playground"
//数据类型字符串
let name1:String = "Lanou"
//字符
let gender:Character = "M"
//整形
let age1:Int = 12
//单精度浮点型
let height:Float = 12.2
//双精度浮点型
let height1:Double = 22.22
//字符串//长度
let length = name.characters.count
//拼接 let name3 = name+"pin\(123)jie"
var name3 = name+"pinjie"
//打印
print("name=\(name3)")
- 字符串插入第几个位置的偏移量
var index = name3.index(name3.startindex,offsetBy:2)
name3.insert(contentOf:"charu".character,at:index)
2.遍历
for item in name3.characters{
}
3.切割
let school1 = "jiningshifan"
let index1 = school.index(school.startIndex,offsetBy:2)
let newschool = school.subString(to:index1)
4.定义一个数组
var arr = [1,2,3]
var arr1 : [Int] = [Int]()
5.遍历一个数组
for(index,item)in arr.enumerated(){
}
6.添加,删除 ,更新,输出 数组元素
arr1.append(2)
let element = arr1.remove(at:0)
arr[1] = 22
print(arr)
7.字典初始化,定义一个空字典.添加,更新,移除
var dic:[Int:Int] = [1:1,2:2]
var dic:[Int:Int] = [Int:Int]()
//添加
dic[3] = 3
//更新
dic[1] = 1
//移除
dic1.removeValue(forkey:1)
8.字符串的范围rang
let student = "zhauangzhiying"
let startindex = student.index(student.startIndex,offsetBy:2)
let endIndex = student.index(student.endindex,offsetBy:-2)
let rang = startIndex..<endIndex;student.subString(with:rang)
9.字典遍历
for item in dic.keys{
}
第二天
- 1 结构体 ,字符串,析构函数,初始化方法
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:123,gender:false,name: "123")
print("stu.name = \(stu.name) stu.gender = \(stu.gender) stu.age = \(stu.age)")
2.类,计算属性,lazy属性,用的时候创建
//类
class Person:NSObject
{
var age:Int?//存储属性
var name:String?//存储属性
//计算属性
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?
}
var p : Person = Person()
print(p.st)
//结构体与类区别
//1⃣️结构体不可继承
//结构体是值类型,
//类是引用类型
struct Dog {
var name:String?
}
var dog :Dog = Dog()
p.name = "张三"
dog.name="旺财"
var p1 = p
var dog1 = dog
p1.name = "李四"
dog1.name = "大黄"
print("p.name = \(p.name) dog.name=\(dog.name)")
3.数组遍历,数组排序冒泡
var names=["zhangsan","lisi","wangwu","zhaoliu"]
for item in names{
print(item)
}
//数组遍历 index索引 item获取得值
for(index,item) in names.enumerated(){
print("index = \\(index),item=\(item)")
}
for a in 1...10{
print(a)
}
//冒泡
var arr = [2,8,4,10,32,20]
for i in 0...arr.count-1 {//循环多少轮
for j in 0..<(arr.count - i - 1){//每伦循环比较多少次
//换位置
if(arr[j] > arr[j+1]){
let t = arr[j]
arr[j]=arr[j+1]
arr[j+1]=t
}
}
}
print(arr)
4.二叉树层次遍历
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()
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("无参数没有返回值")
}
//2.无参数有返回值
func fun2() -> Int {
print("无参数有返回值")
return 0
}
//3 有参数有返回值
func fun3(parm:Int)->Int{
print("有参数有返回值")
return 0
}
//4 有参数无返回值
func fun4(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,5)
func fun6( a:inout Int,b:inout Int){
var temp = a
a = b
b = temp
}
var a=10
var b=20
fun6(a: &a,b: &b)
print("a = \(a),b = \(b)")
第三天
- 1协议
//协议类似于接口,别的类遵守协议
//定义了一个协议
protocol PersonProtocol {
func study()
}
//可选协议
@objc protocol StudentProtocol{
@objc optional func happy()
var name:String{get set}
}
class person:NSObject,PersonProtocol,StudentProtocol{
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)")
//练习
//面向协议编程
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
}
}
2枚举
//枚举
enum Season :String {
case Spring = "春天"
case Summer = "夏天"
case auto = "秋天"
case winter = "冬天"
// case 河南,内蒙古,山东
}
let season = Season.Spring
//print(Season.auto.rawValue)
//遍历
//switch case必须包含所有的情况,否则default
switch season{
case .Spring:
print("春天")
case .Summer:
print("夏天")
case .auto:
print("秋天")
case .winter:
print("冬天")
default:
print("输出两个季节")
}
let a:Int = 10
switch a{
case 1...20:
print("包含10")
case 2...30:
print("包含10")
default:
print("")
}
//枚举不用default,switch case 用default
第四天
- 1,类的定义
//类的定义
/* class 类名:父类*/
class Node: NSObject {
//?可选类型(可能有值可能有值)
//! 强制拆包(解析)
var data:Int!
var leftChild:Node?
var rightChild:Node?
}
- 2.泛型
//可以打印 数字,字符串,字符
func MyprintNumber(parm:Int){
print(parm)
}
func MyprintString(parm:String){
print(parm)
}
func MyprintCharacter(parm:Character){
print(parm)
}
//使用泛型之后的一个就可以了
//func Myprint<T>(parm:T){
//print(parm)
//}
func Myprint<T:Collection>(parm:T){
print(parm)
}
let a = [1,2,3]
Myprint(parm: a)
3.闭包
func sort(num:Int,num1:Int)->Bool{
//如果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,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)
4,代理
//协议是别人遵守的
protocol GrilProtocal {
func MDan()
func happy()
}
//delegate代理
class Girl : NSObject{
//设置代理值
var delegate : GrilProtocal?
//shopBegin
func shopBegin() {
if let _ = delegate {
delegate?.happy()
}
}
//购物完毕
func shopOver() {
if let _ = delegate {
self.delegate?.MDan()
}
}
}
class Boy : NSObject,GrilProtocal{
func happy(){
print("高兴")
}//买单
func MDan(){//实现协议
print("买单")
}
}
let boy : Boy = Boy()
let girl : Girl = Girl()
girl.delegate = boy
girl.shopOver()
girl.shopBegin()
5,自己练习的代理文件
protocol name {
func kaishi()
func xiazai()
func gc()
func jieshu()
}
class File:NSObject{
//设置代理值
var delegate : name?
//开始
func downloadbegin(){
if let _ = delegate{
delegate?.kaishi()
}
}
//下载
func downloadxiazai(){
if let _ = delegate{
delegate?.xiazai()
}
}
//下载中
func downloadzhong(){
if let _ = delegate{
delegate?.gc()
}
}
//结束
func downloaderr(){
if let _ = delegate{
delegate?.jieshu()
}
}
}
class MakeFile:NSObject,name{
func kaishi(){
print("开始")
}
func xiazai(){
print("下载")
}
func gc(){
print("下载中")
}
func jieshu(){
print("结束")
}
}
let a :File = File()
let b:MakeFile = MakeFile()
a.delegate = b
a.downloadbegin()
a.downloadxiazai()
a.downloadzhong()
a.downloaderr()
第五天
- UIview控件
- 1.AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//程序启动
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//初始化window
let frame = UIScreen.main.bounds
self.window = UIWindow(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.swift
class ViewController: UIViewController {
override func viewDidLoad() {
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)
//frame相对于父视图
let subview1 = UIView()
subview1.frame = CGRect(x: 140, y: 240, width: 100, height: 100)
subview1.backgroundColor = UIColor.yellow
self.view.addSubview(subview1)
//center
let subview3 = UIView()
self.view.addSubview(subview3)
subview3.frame = CGRect(origin: self.view.center, size: CGSize(width: 20, height: 20))
subview3.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, 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.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
subview3.addSubview(subview4)
//tag值使用2000以上,100以下系统用
subview4.tag = 10001
let Tagview = subview3.viewWithTag(10001)
print("subview4 = \(subview4),Tagview = \(Tagview)")
//subview4.isHidden = true //隐藏
//用户交互
self.view.isUserInteractionEnabled = true
//superview是父试图
print("subview = \(subview4.superview),subview3 = \(subview3)")
for item in self.view.subviews{
//从父试图上移除
// item.removeFromSuperview()
print(item)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("点击了当前控制器")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
- MyviewController.swift
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}