Swift笔记

17 篇文章 0 订阅

Swift笔记

1 mutating 修饰可更改的方法
2 value1 ?? value2 == value1为nil时 取value2
3 单例写法
”private let Instance = MyManager()
”class MyManager{
” class var sharedManager : MyManager{
” return Instance
” }
”}
4 typealias(类型别名) 和 范型接口
5 @UIApplicatiomMain 替换掉系统默认的委托 做些需要整个应用处理的事情
”import UIKit
”class MyApplication : UIApplication {
” override func sendEvent(event: UIEvent!){
” super.sendEven(event)
” print(“EVent sent : (event)”)
” }
”}
” UIApplicationMain(C_ARGC, C_ARGV, NSStringFromClass(MyApplication), NSStringFromClass(AppDelegate))

4 protocol
5 C API Swift API
const Type UnsafePointer
Type UnstafeMutalerPointer

6 AnyClass 远类型 和 .self
”let usingVCTypes : [AnyClass] = [MusicViewController,self,
”AlbumViewController.self
”]
”func setUpViewControllers(VcTypes:[AnyClass]){
”let vcType in VcTypes{
” if vcType is UIViewController.Type{
” let vc = (vcType as UIViewController.Type).new()
” printIn(vc)
” }
”}
”}

7 protocol 的 Protocol 类似 .Type

8 is ——> isKindOfClass

9 断言 —> assert
” func convertTokeLvin(# celsius: Double) -> Double{
” assert(celsiu> absoulteZeroInCelsius, “输入的摄氏温度不能低于绝对零度”)
” return celSius - absoulteZeroInCelsius
” }

10 find
” let arr = [1,2,3]
” if let index = find(arr, 2){
” printIn(”找到了”)
” }else{
” printIn(“没有找到”)
” ‘}

11 数字
” Int.max Int.min infinity NaN

12 NSNull
” swift 里 将NSNull 转为 nil

13 printInLog
” func printLog (message:T ,
” file:String = FILE,
” method: String FUNCTion,
” line: Int _ LINE_){
” #if DEBUG
” println(“(file:.lastPathComponent)[(line),(method): (message)]”)
” #endif
” }

14 宏定义 define
” swift 没有宏定义了
” var M_PI : Double {get}

15 String and 字符串(characters)
” isEmpty
” as
” var emptypeString = “”
” emptypeString.isEmpty

在实际编译的时候 Swift 编译器会优化字符串的使用 使实际的复制只发生在绝对必要的条件下
”let str:String = “awdwadwadaw”
”for character in str.characters {
” print(character)
”}

”String
”1 StartIndex
”2 endIndex
”3 index(before:)
”4 index(after:)
”5 index(_: offsetBy)
”6 insert(_: at: ) 可以在一个字符串的指定索引插入一个字符串
”7 insert(contentOf: at:) 可以在一个字符串指定的位置插入一个字符串
”8 remove(at:)
”9 removeSubrange(_:) 可以在一个字符串的指定索引位置删除一个自字符串

”字符串的比较
”hasPrefix(:) / hasSuffix(:) //前后缀的比较

16 array 数组
”如果我们同时需要每个项的值和索引值 可以使用 enumerated()方法来进行数组遍历 enumerated() 返回一个由每一个数据项索引值和数据只组成的元祖
” for (index , value) in array.enumerated{
” print(“item(String(index+1):(value))”)
” }

17 Set
” insert(_:) //返回元祖(true , value)
” remove(_:) //返回元祖(true , value)
” removeAll()
” contains(_:) //查找特定的值 (Bool)
” sorted() //排序 ”<”
” instersection(_:) //交集新的集合
” symmetricDifference(_:) //交集的反向
” union(_: ) //并集
” subtracting(_:) //补集

18 switch
” case (0,0) //元祖匹配
” case (_ , 12) // _ 代表任意
” case (1..<10) //区间匹配
” case (x,y)where x == y: //利用where 来判断必要的条件

19 控制转移语句
”contiune // 停止本次循环 进入下一次循环
” break //结束循环
” fallthrough // 简单的使代码继续链接到下一case中的代码
” return
” throw

20 可变参数
” func arithmeticMean(_numbers: Double…) -> Doulble{ //可变参数的值在函数体中变为一个数组
” for number in numbers{
” }
” }

21 函数类型
” func addTwoInts(_ a:int , _ b:Int) ->Int{
” }
” func multiplyTwoInts(_ a: Int, _ b: Int ) ->Int{
” }
” 这两个的函数类型都是 (Int,Int)->Int
” var mathFunction : (Int ,Int) -> Int = addTwoInts
” var mathFuction : (Int ,Int )->Int = multiplyTwoInts

” let num : Int = addTwoInts(1,2)

” //函数类型作为参数类型
” func printMathResult(_ mathFunction: (Int ,Int)->int, a : int b:Int){
” }
” // 函数类型作为返回值类型
” func chooseSetFunction(backward:Bool) ->(Int,Int)->Int{
” }
” //嵌套函数
” 返回局部的函数 给 外界调用

22 排序 sorted
” let array = [1,2,3,4,5,9,34,23,45,98]
” var arrayStr = array.map({ 0 0})
” print(arrayStr)
” let arr = array.sorted { (a, b) -> Bool in
” return a>b

” }

” let arrat = arr.sorted(by: { 0< 1})

23
”internal
internal访问级别所修饰的属性或方法在源代码所在的整个模块都可以访问。如果是框架或者库代码,则在整个框架内部都可以访问,框架由外部代码所引用时,则不可以访问。 如果是App代码,也是在整个App代码,也是在整个App内部可以访问

24 is & as
”is 返回的是 true false
”as 返回的为 self nil

25 字面量表达式
FILE LINE COLUNM __FUNCTION

26 捕获说明
” weak unowned unowned(safe) unowned(unsafe)

27 swift 枚举值(enum Values) 关联值
” swift 关联值
” 支持 整型(Integer)
” 浮点数(Float Point)
” 字符串(String)
” 布尔类型(Boolean)

28 swift 枚举附加值的使用demo

T  U  M  都是范型代表任意的类型
enum Extension {
case text1(key:T,Value:U)
case text2(model:M)
}

设置值
.text1(key:"key",Value:"任意啊")
.text2(model:model)

获取址

switch Extension.type(
    case let text1(key,value):
    print(key,valye)
    break 
    case let text2(model):
    print(model)
    break

29
” /// 去掉下划线 失败 改为设置下划线的颜色为 clear 不知道为什么回设置失败? 后面去研究下
” attributedString.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.styleNone.rawValue, range: NSMakeRange(0,attributedString.length))

30
去掉返回值没有使用的警告⚠️
1 @discardableResult
2 —=

因为现在在写android kotlin 所以空闲时间不多 后续会持续更新 Swift 和 Kontlin 。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值