安路的技术博客

Nothing in life is to be feared. It is only to be understood

Swift语法小技巧

| Comments

判断某个对象是否响应了某个方法

没有参数的:

1
2
3
if(view.respondsToSelector(#selector(startAnimating))){
        
}

一个参数的:

1
2
3
 if(self.modeView.respondsToSelector(#selector(hideAnimated(_:)))){
          
 }

多个参数的:

1
2
3
 if(self.modeView.respondsToSelector(#selector(setProgress(_:animated:)))){
            
 }

第一个参数用_,第二个先参数名字,中间没有逗号

Swift中如何使用 #Debug

在oc中可以直接使用

1
2
3
4
5
#if DEBUG 
  let a = 2
#else 
  let a = 3
#endif

但是swift没有引进预处理指令,但并不代表不可以使用预处理指令。

首先要在 setting中设置,在Swift Compiler - Custom Flags,找到 Other Swift Flags,添加如下标记 -D DEBUG,在release模式下添加 -D RELEASE,如图: logo

现在就可以正常的使用#if DEBUG了

UIColllectionView

注册UIcollectionViewCell

1
    collectionView.registerClass(NN_XueTangCell.self, forCellWithReuseIdentifier: cellId)

Comments