swift 4 使用小結 與OC差異比較大的寫法

2021-08-20 13:00:45 字數 3906 閱讀 5589

lazy var imageview:uiimageview = ()
lazy var button:uibutton = ()

@objc func buttonclick(_ button:uibutton) -> ()

protocol imageviewdelegate: class
@objc protocol imagecelldelegate: nsobjectprotocol
weak

vardelegate:imagecelldelegate?

注意:**繼承的父類 class和nsobjectprotocol 的區別

**繼承的父類nsobjectprotocol的時候,判斷**是否實現某個方法responds(to: #selector()能提示處理,繼承自class則沒法判斷

imagecelldelegate.實現的方法去判斷!!!

@objc func buttonclick(_ button:uibutton) -> () 

ifdelegate.responds(to: #selector(imagecelldelegate.imagecellbuttonclick(imagecell:content:)))

}

//判斷某個類

func scrollviewdidscroll(_ scrollview: uiscrollview)

}//判斷某個類及其子類

func scrollviewdidscroll(_ scrollview: uiscrollview)

}

//重新整理所有

self.tableview

.reloaddata()

//重新整理分組

self.tableview

.reloadsections([1], with: .none)

//重新整理某乙個cell或者幾個cell

self.tableview

.reloadrows(at: [indexpath], with: .none)

class func gettopiccontentimageurllink(_ range: nsrange, _ pattern: string = "let regularresults = regularexpression.matches(in: content, options: , range: nsmakerange(0, content.count))

for textcheckingresult in regularresults }}

return imageurlarr

}

let content = (newstring as nsstring).addingpercentescapes(using: string.encoding.utf8.rawvalue)!
//執行緒通訊

dispatchqueue.global().async

}//延遲操作 dispatchtime 的精度是納秒

dispatchqueue.main

.asyncafter(deadline: dispatchtime.now() + 10)

dispatchqueue.global().asyncafter(deadline: dispatchtime.now() + 10)

//dispatchwalltime 的精度是微秒

dispatchqueue.global().asyncafter(walldeadline: dispatchwalltime.now() + 2.0)

let arr = [1,2,3,4,5]

//是個切片陣列和陣列之間可以轉化

let arrslice = arr[0..<3]

let subarr = array(arrslice)

//通知名稱

let zqpcouponcellselectedcoupon: notification.name = notification.name(rawvalue: "zqpcouponcellselectedcoupon")

//發通知

notificationcenter.default.post(name: zqpcouponcellselectedcoupon, object: couponmodel)

//接通知

notificationcenter.default.addobserver(self, selector: #selector(selectedcoupon(_ :)), name: zqpcouponcellselectedcoupon, object: nil)

//響應事件

@objc func selectedcoupon(_ notication: notification)

//當前時間---毫秒級 時間戳 - 13位

let timeinterval: timeinterval =

date().timeintervalsince1970

let currentmillisecond = clonglong(round(timeinterval*

1000))

print(currentmillisecond)

`1532068608613`

//當前時間---秒級 時間戳 - 10位

let timeinterval: timeinterval =

date().timeintervalsince1970

let currentsecond = int(timeinterval)

print(currentsecond)

`1532068723`

屬性前+@objc,否則字典轉模型不成功

class personmodel: nsobject
static

let sharedinstance = zymanagercontroller()

//oc

#import

//swift

import uikit.uigesturerecognizersubclass

//注意,確保string有值,否則每一步轉換,都要guard防衛

let childrencontrollerstring: string = item[rootvcname] as! string

let namespace = bundle.main.infodictionary!["cfbundleexecutable"] as! string

let childrencontrollerclass = nsclassfromstring(namespace + "." + childrencontrollerstring)

let childrencontrollertype = childrencontrollerclass as! uiviewcontroller.type

let childrencontroller = childrencontrollertype.init()

swift4 學習筆記 二

用函式來定義乙個功能 定義乙個函式需要在函式名後面用小括號 來新增引數,用 來區分引數型別,名稱和返回值的型別 func greet name string,day string string let greetstring greet name snow day monday print gree...

Swift 4 流程控制

if語句 判斷條件不需要小括號,條件執行體必須要放在花括號中 var int6 int 6 if int6 0 print int6 else print 666666666 switch語句 switch中不需要強制寫break語句,但是在每個case裡必須有一條語句 fallthrough可以在...

Swift4 模式, 列舉型

建立 2018 03 05 完成 2018 03 05 更新 2018 03 10 改變標題 swift4 模式 swift4 模式,列舉型 補充rawrepresentable協議 任務表 todo 元組 taple 與switch 模式匹配 pattern matching 檢驗構造和值是否一致...