1. swift print log 只在debug模式下打印
- 创建DebugLogTool类
class DebugLogTool: NSObject {
static func debugLog(_ item: Any) {
#if DEBUG
print(item)
#else
#endif
}
}
- 修改Setting Other Swift Flags
Debug : -D DEBUG
Release :
2. DispatchQueue.once 多线程只执行一次方法
extension DispatchQueue {
private static var _onceTracker = [String]()
public class func once(token: String, block: () -> ()) {
objc_sync_enter(self)
defer {
objc_sync_exit(self)
}
if _onceTracker.contains(token) {
return
}
_onceTracker.append(token)
block()
}
func async(block: @escaping ()->()) {
self.async(execute: block)
}
func after(time: DispatchTime, block: @escaping ()->()) {
self.asyncAfter(deadline: time, execute: block)
}
}
关键字的使用
-
修改结构体 modify struct
-
使用mutating 关键字才可以修改结构体定义的内容
mutating func () {
self.xxx = xxx;
}
-
init 初始化时候的关键字使用
-
convenience 便利构造函数,里面需要调用self.init
convenience init (xxx , xxx){
sele.init(xxx,xxx)
}
-
override 重载父类init方法
override init(name : String ){
super.init(name: name + " " + name)
}
-
下标方法
-
subscripe 关键字来声明一个下标函数
subscripe (index : Int) -> Point{
assert (index >= 0 , "index out of range")
}
-
assert 断言函数
assert (index >= 0 , "index out of range")
-
for in 循环
for i in 2.stride(to :20 by: 2){
//从2 到 20(开区间) 每次+2 through(闭区间)
}
-
类别名
-
typealias 在类里定义
class xxx : (xxx){
typealias xxx = Int
}
-
assocaitedtype 在协议里定义,之后在类型里实现的时候,需要用 typealias 来重新赋值
protocol xxx{
associatedtype xxx;
}
class yyy: xxx{
typealias xxx = Double
}
-
按钮点赞动画放大效果
let k = CAKeyframeAnimation(keyPath: "transform.scale")
k.values = [0.1,1.0,1.5]
k.keyTimes = [0.0,0.5,0.8,1.0]
k.calculationMode = CAAnimationCalculationMode.linear
button.layer.add(k, forKey: "SHOW")