2020.6.15 学习笔记

概述阶段

MVVM架构模式
MVVM多了个ViewModel层。
数据请求完成后,先进入到ViewModel层,该转译转译,该封装封装,然后在由接口返回。
View层负责把组合后的model的各个属性与View中的各个组件绑定,比如A组件绑定model的a属性。
与MVC最大的不同:
1.ViewController不在具有数据处理能力,仅仅负责数据与控件的绑定。
2.ViewModel层主要处理数据、处理一些小的业务逻辑。功能单一化,专业换。
3.如果需求不涉及UI位置的变化,仅数据内容更改,往往只需要去修改ViewModel层就好了,更为便捷

Swift知识点:
1.String能正确识别Unicode编码编码
2.字符串“33.0”无法强转为int类型
3.字符串转数字,结果为可选值(带?),Swift中不像OC会自动赋0,转换失败就是nil

代码阶段:

String和Character、String识别Unicode编码

//定义Character数组
let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"]
// 字符数组转字符串
let catString = String(catCharacters)

//在字符串中存入Unicode编码将被自动识别
//由于指定类型,所以为Character
let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"
//不指定类型的话默认就是String
let regionalIndicatorForUS2 = "\u{1F1FA}\u{1F1F8}"
//🇺🇸
print(regionalIndicatorForUS)
//🇺🇸 1   Unicode编码长度被正确识别
print(regionalIndicatorForUS2,regionalIndicatorForUS2.characters.count)

String的遍历、截取、转编码

//str的遍历方式1
for character in "Dog!🐶".characters {
    print(character)
}
//str的遍历方式2
let str = "Dog!🐶"
for index in str.characters.indices {
    //这里的index并不是一个真真的int,不可直接使用
    print("\(str[index])")
}

// 截取字符串
let baseStr = "Guten Tag!"
//截取首个字符
print(baseStr[baseStr.startIndex])
//截取前三个字符(string中发生截取常需要用到Index结构体)
print(baseStr.substring(to: baseStr.index(baseStr.startIndex, offsetBy: 3)))
// 截取一段(起始位置为2,结束位置为endIndex-1)
print(baseStr[baseStr.index(baseStr.startIndex, offsetBy: 2)..<baseStr.index(baseStr.endIndex, offsetBy: -1)])

var welcome = "hello,World"
// 在指定位置插入一个字符
welcome.insert("!", at: welcome.endIndex)
//输出结果:hello,World!
print(welcome)
//在指定位置之前插入一个字符串(依然涉及index结构体)
welcome.insert(contentsOf:" there".characters, at: welcome.index(before: welcome.endIndex))
//输出结果:hello,World there!
print(welcome)
//移除最后一个字符(remove仅能移除一个字符)
welcome.remove(at: welcome.index(before: welcome.endIndex))
//输出结果:hello,World there
print(welcome)
//移除最后6个字符
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
//hello,World
print(welcome)
//检查前缀和后缀(前缀:hasPrefix  后缀:hasSuffix)
if welcome.hasSuffix("World"){
}
//把每个字符转编码
for codeUnit in welcome.utf16 {
    print("\(codeUnit) ", terminator: "")
}

余数运算函数(truncatingRemainder)

//取余数运算函数(truncatingRemainder)
print(41.truncatingRemainder(dividingBy: 4))
//甚至可以取浮点数的余数
let z =  CGFloat(7.1).truncatingRemainder(dividingBy: 1.2)
print(z)

Self 和 扩展
Self 用于某个对象所归属的类,无法确定其具体类型

//需求:给所有数字类型,扩展一个平方的函数,返回自己的操作
// 定义个协议
protocol NumberProtocol{
}
// 扩展实现协议
extension Int:NumberProtocol{
}
extension Double:NumberProtocol{
}
extension Float:NumberProtocol{
}
// 给协议扩展方法
extension NumberProtocol{
    // 我们不确定返回的Self 到底是什么类型
    func squareValue()-> Self{
        if self is Int{
            let n = self as! Int
            return n * n as! Self
        }
        if self is Double{
            let n = self as! Double
            return n * n as! Self
        }
        if self is Float{
            let n = self as! Float
            return n * n as! Self
        }
        //走到这里的话大部分情况下会报错,说明输入的类型不正确
        return 0 as! Self;
    }
}

//9.0601
print(Float(3.01).squareValue())
//11.8336
print(3.44.squareValue())
//9
print(3.squareValue())

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容