swift中的巨集

2021-08-15 14:17:36 字數 1609 閱讀 1073

swift本身不支援巨集定義,因此相關的操作採用的是常量代替

1.建立乙個swift檔案專門用於存放代替巨集的常量

2.常量->巨集定義

let screen_height = uiscreen.mainscreen().bounds

.height

let screen_width = uiscreen.mainscreen().bounds

.width

let fit_width = uiscreen.mainscreen().bounds

.size

.width / 375

let fit_height = uiscreen.mainscreen().bounds

.size

.height / 667

let font_30 = uifont.systemfontofsize(15)

let font_60 = uifont.systemfontofsize(30)

let font_140 = uifont.systemfontofsize(70)

let systemversion = (uidevice .currentdevice().systemversion as string)

3.全域性函式->巨集函式

func x(object: uiview) ->cgfloat 

func y(object: uiview) -> cgfloat

func width(object: uiview) -> cgfloat

func height(object: uiview) -> cgfloat

func centerx(object: uiview) -> cgfloat

func centery(object: uiview) -> cgfloat

//rgb

func rgb(r:cgfloat,g:cgfloat,b:cgfloat)->uicolor

由於是全域性的,使用起來直接呼叫即可

此時你可能會對巨集定義提出疑問,如果有巨集定義來進行編譯控制,#ifdef之類的處理如何做,swift在這點上做了處理,但是需要借借助於編譯符號

1.設定編譯符號

3.效果

swift本身不支援巨集,但是我們可以通過常量+編譯符號實現一樣的效果

swift中的巨集

swift編譯器不包含預處理器。因此,不能向oc一樣用 define 定義,在swift中,可以用全域性常量代替巨集定義。1.新建立乙個swift檔案,新增標頭檔案 import uikit 2.新增常量 方法 let screen width uiscreen.main.bounds.size.w...

Swift中巨集 define的替代方案

我們在swift中已經沒有 define這種巨集定義了,那麼替代方案有哪些呢?1 let宣告常量 let is ios7 uidevice.currentdevice systemversion as nsstring doublevalue 7.0當然這種代替方案 只適用於一般的常量巨集定義,如果...

Swift3 0 中的仿oc中的巨集定義檔案

swift中取消了 define這種巨集定義了,可以用全域性let來宣告常量來取代 首先建立乙個common.swift 檔案來存放你所定義的巨集定義內容 當前系統版本 let kversion uidevice.current systemversion as nsstring floatvalu...