qmui-ios-codesnippets
qmui-ios-codesnippets 是一個 QMUI 團隊日常工作中整理出來的用於 Xcode 的 iOS 通用代碼片段集,其中也包含若干專用於 QMUI for iOS 框架的代碼片段。
整理這個代碼片段集的初衷有以下幾點:
- 我們發現由於 Xcode 本身的功能不足,導致我們經常在重寫一些系統父類方法時容易忘了調用
super
,從而出現一些很難排查的詭異bug。 - Xcode 雖然有模糊匹配的代碼提醒,但代碼提醒只能幫你寫方法名,而code snippets 還可以幫你填充一些默認的方法實現,或者直接移動光標到方法體內,省去幾次光標操作。
- 一些常用的寫法本身語法可能比較複雜,難以記憶,例如實現一個類的單例、使用
swizzle
來重寫系統控件的方法、block 在不同地方的語法不同等。 - 一些代碼本身看似簡單,但由於特別常用,所以使用 code snippets 可以大大節省時間。
使用方式
Xcode 的 Code Snippets 文件存放於 ~/Library/Developer/Xcode/UserData/CodeSnippets
目錄,只要直接把 *.codesnippets
文件放到這個目錄下(若沒有則自己創建),重啓 Xcode 即可生效。
爲了方便更新,建議直接將 QMUI iOS CodeSnippets clone 到這個目錄內(注意,不是在 CodeSnippets 裏創建一個 QMUI 的目錄,這裏不支持子目錄):
其中以 QM_
前綴開頭的文件是通用的 Code Snippets,以 QMUI_
前綴開頭的文件是專用於 QMUI for iOS
框架的代碼片段。在下方的快捷鍵彙總裏,QMUI 的代碼片段將會以QMUI的形式標記出來。
注意,Xcode 對每一段 Code Snippet 都有規定適用的語言(Objective-C、Objective-C++、Swift、...)和作用域(如 Class 的 Interface 定義內、Class 的 Implementation 內、方法體內、...),所以測試某段 Code Snippet 不生效時請注意你當前是否處於不匹配的位置。
快捷鍵彙總
NSObject
pa
- 定義一個assign
的 propertypar
- 定義一個assign, readonly
的 propertypc
- 定義一個copy
的 propertyps
- 定義一個strong
的propertypsr
- 定義一個strong, readonly
的propertypw
- 定義一個weak
的propertypwr
- 定義一個weak, readonly
的propertypropertySwizzleAssign
- 用swizzle
的方式定義一個assign
的propertypropertySwizzleCopy
- 用swizzle
的方式定義一個copy
的propertypropertySwizzleStrong
- 用swizzle
的方式定義一個strong
的propertypropertySwizzleWeak
- 用swizzle
的方式定義一個weak
的propertysharedInstance
- 爲當前類創建一個實現單例功能的sharedInstance
方法exchangeImplementation
- 重寫當前類的load
方法並在其中用swizzle
替換方法實現exchangeImplementation_QMUI
- 用 QMUI 重寫當前類的load
方法並用ExchangeImplementations()
函數替換方法的實現exchangeMultipleImplementations_QMUI
- 用 QMUI 重寫當前類的load
方法並用ExchangeImplementations()
函數批量替換多個方法的實現override_void_nonArgv
- 用 QMUI 的OverrideImplementation()
重寫指定 class 的某個無返回值、無參數的方法實現override_void_argv
- 用 QMUI 的OverrideImplementation()
修改指定 class 的某個無返回值、帶一個參數的方法實現override_return_nonA
- 用 QMUI 的OverrideImplementation()
修改指定 class 的某個帶返回值、無參數的方法實現override_return_argv
- 用 QMUI 的OverrideImplementation()
修改指定 class 的某個帶返回值、帶一個參數的方法實現extend_void_nonA
- 用 QMUI 的ExtendImplementationOfVoidMethodWithoutArguments()
修改指定 class 的某個無返回值、無參數的方法實現extend_void_argv
- 用 QMUI 的ExtendImplementationOfVoidMethodWithSingleArgument()
修改指定 class 的某個無返回值、帶一個參數的方法實現extend_return_nonArgv
- 用 QMUI 的ExtendImplementationOfNonVoidMethodWithoutArguments()
修改指定 class 的某個帶返回值、無參數的方法實現extend_return_argv
- 用 QMUI 的ExtendImplementationOfNonVoidMethodWithSingleArgument()
修改指定 class 的某個帶返回值、帶一個參數的方法實現
Block
blockArguments
- 聲明一個用於方法參數的 blockblockproperty
- 聲明一個用於 property 的 blockblocktypedef
- 用typedef
定義一個 blockblockvar
- 定義一個作爲局部變量的 block
Method & Function
fnv
- 定義一個返回值爲void
的方法fnv:
- 定義一個返回值爲void
且帶參數的方法fnblock
- 定義一個返回值類型爲 block 的方法fnv_handleEvent
- 定義一個用於UIControl
事件回調的方法fnv_longPress
- 定義一個用於UILongPressGestureRecognizer
的回調方法(你就不用每次都去拼寫那個很長的手勢名字了)fnv_pan
- 定義一個用於UIPanGestureRecognizer
的回調方法fnv_tap
- 定義一個用於UITapGestureRecognizer
的回調方法
UIView
setFrame
- 爲UIView
設置frame
setFrame_QMUI
- QMUI 使用像素對齊的CGRectFlatMake()
爲UIView
設置frame
setFrameX
- QMUI 使用CGRectSetX()
修改UIView
的frame.origin.x
setFrameY
- QMUI 使用CGRectSetY()
修改UIView
的frame.origin.y
setFrameXY
- QMUI 使用CGRectSetXY()
修改UIView
的frame.origin
sizeThatFits
- 爲當前 view 創建sizeThatFits:
方法layoutSubviews
- 展開layoutSubviews
方法updateConstraints
- 展開updateConstraints
方法getWidth
- 展開CGRectGetWidth()
getHeight
- 展開CGRectGetHeight()
getMinX
- 展開CGRectGetMinX()
getMinY
- 展開CGRectGetMinY()
addtarget
- 調用UIControl addTarget:action:forEvents:
方法setImageForButton
- 爲UIButton
設置圖片setTitleColorForButton
- 爲UIButton
設置文字顏色setTitleForButton
- 爲UIButton
設置文字
UITableView
initWithStyle
- 展開initWithStyle:
方法initWithStyleForCell
- 展開UITableViewCell initWithStyle:reuseIdentifier:
方法tableViewDelegate
- 展開常用的幾個UITableViewDelegate
方法numberOfSectionsInTableView
- 展開numberOfSectionsInTableView:
方法numberOfRowsInSection
- 展開tableView:numberOfRowsInSection:
方法cellForRowAtIndexPath
- 展開tableView:cellForRowAtIndexPath:
方法heightForRowAtIndexPath
- 展開tableView:heightForRowAtIndexPath:
方法didSelectRowAtIndexPath
- 展開tableView:didSelectRowAtIndexPath:
方法
UICollectionView
collectionViewDelegate
- 展開常用的幾個UICollectionViewDelegate
方法numberOfSectionsInCollectionView
- 展開numberOfSectionsInCollectionView:
numberOfItemsInSection
- 展開collectionView:numberOfItemsInSection:
cellForItemAtIndexPath
- 展開collectionView:cellForItemAtIndexPath:
sizeForItemAtIndexPath
- 展開collectionView:layout:sizeForItemAtIndexPath:
方法didSelectItemAtIndexPath
- 展開collectionView:didSelectItemAtIndexPath:
方法didDeselectItemAtIndexPath
- 展開collectionView:didDeselectItemAtIndexPath:
方法
UIViewController
initWithNib
- 展開initWithNibName:bundle:
方法didInitialize
- 展開某些 QMUI 控件提供的didInitialize
方法didInitializeWithStyle
- 展開 QMUICommonTableViewController 的didInitializeWithStyle:
方法loadView
- 展開loadView
方法viewDidLoad
- 展開viewDidLoad
方法viewWillAppear
- 展開viewWillAppear:
方法viewDidAppear
- 展開viewDidAppear:
方法viewWillDisappear
- 展開viewWillDisappear:
方法viewDidDisappear
- 展開viewDidDisappear:
方法viewDidLayoutSubviews
- 展開viewDidLayoutSubviews:
方法updateViewConstraints
- 展開updateViewConstraints:
方法addChildViewController
- 在當前UIViewController
裏添加childViewController
removeFromParentViewController
- 將childViewController
從當前的UIViewController
裏移除initSubviews
- QMUI 展開initSubviews
方法setupNavigationItems
- QMUI 重寫QMUICommonViewController
裏的setupNavigationItems
方法setupToolbarItems
- QMUI 重寫QMUICommonViewController
裏的setupToolbarItems
方法
UIBarButtonItem
backItem
- 用 QMUI 方法生成一個返回按鈕boldTitleItem
- 用 QMUI 方法生成一個文字加粗的導航欄按鈕closeItem
- 用 QMUI 方法生成一個導航欄上的關閉圖標按鈕imageItem
- 用 QMUI 方法生成一個導航欄上的圖片按鈕titleItem
- 用 QMUI 方法生成一個導航欄上的文字按鈕
Other
pragma
- 展開一個用於 Xcode 導航的#pragma mark -
宏if11
- 展開一個 @available(iOS 11, *) 的 if 判斷externRefInH
- 在*.h
文件裏聲明一個extern const
的指針externRefInM
- 在*.m
文件裏爲一個extern const
的指針賦值externValueInH
- 在*.h
文件裏聲明一個extern const
的值變量externValueInM
- 在*.m
文件裏爲一個extern const
的變量賦值static reference
- 定義一個static
的指針static
- 定義一個static
的值變量__weakSelf
- 定義一個weak
的self
指針__strongSelf
- 將weakSelf
指針改爲strong
的self
指針logCallStackSymbols
- 用NSLog
打出當前的方法調用棧信息timeConsuming
- 展開一段用CACurrentMediaTime()
來計算方法耗時的代碼