美文网首页
Swift中的dynamic关键字?

Swift中的dynamic关键字?

作者: 番茄炒西红柿啊 | 来源:发表于2017-02-14 17:29 被阅读1282次
animation = CABasicAnimation(keyPath: "path")
        animation.fromValue = startPath
        animation.toValue = endPath

        //疑问1?
        animation.delegate = AnimationDelegate {
            layer.mask = nil
            self.completion()
            self.animation.delegate = nil
        }


//AnimationDelegate.swift
import QuartzCore

class AnimationDelegate {
    private let completion: () -> Void

    init(completion: () -> Void) {
        self.completion = completion
    }

    //疑问2?
    dynamic func animationDidStop(_: CAAnimation, finished: Bool) {
        completion()
    }
}

//疑问1中的AnimationDelegate是一个类实例化后的对象吗?怎么大括号中还能执行语句?还是其他什么意思?
//疑问2中的dynamic什么意思?

疑问1
 animation.delegate = AnimationDelegate {
    layer.mask = nil
    self.completion()
    self.animation.delegate = nil
}
其实就是

animation.delegate = AnimationDelegate(completion: {
    layer.mask = nil
    self.completion()
    self.animation.delegate = nil
})

这样就调用了AnimationDelegate的 init 函数,传入的参数是个闭包(closure)。

在 Swift 中有个小的语法糖,假如闭包作为函数的最后一个参数,可以将闭包的大括号提到外面,并且只有一个参数时可以省略函数调用的小括号。这样就变成了最初的写法了。

这个闭包的语法糖,我挺喜欢的,可以使得代码更干净。Swift 这个特性用来做 DSL 也会很方便。再举个例子,常见的 Grand Central Dispatch 可以写成:

dispatch_async(dispatch_get_main_queue()) {
// do something
}
这也是将闭包的大括号提出到外面。但因为函数有两个参数,就不能省略掉函数调用的小括号。

作者:黄兢成
链接:https://www.zhihu.com/question/36999574/answer/69947633
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

疑问2
Objective-C 中的所谓函数调用,其实是发送消息。比如

[obj domething:arg1];

会被编译成

objc_msgSend(obj, @selector(something:), arg1);

从字符串"domething:"动态查找到对应的方法地址。Objective-C 的消息发送是完全动态的,动态特性对于构建UI库很有用。(用静态的语言比如 C++, C 之类去做 UI 库会难得多)。

但是 Swift 不同,Swift 中的函数可以是静态调用,静态调用会更快。当函数是静态调用的时候,就不能从字符串查找到对应的方法地址了。这样 Swift 跟 Objective-C 交互时,Objective-C 动态查找方法地址,就有可能找不到 Swift 中定义的方法。

这样就需要在 Swift 中添加一个提示关键字,告诉编译器这个方法是可能被动态调用的,需要将其添加到查找表中。这个就是关键字 dynamic 的作用。

你假如去掉 dynamic 关键字,animationDidStop:finished: 就不会被调用了。

另一个方法是在 Swift 中直接或间接继承 NSObject,比如

class AnimationDelegate : NSObject
当继承 NSObject 时,就认为是 Objective-C 的类,就按照 Objective-C 的方式处理。注意就算是继承 NSObject,但将方法标记成私有 private,也是不能动态找到方法地址的。

在swift中,采用KVO模式对属性进行值得变化监听的时候,也需要用到dynamic关键字:

class Model: NSObject {
    
    dynamic var contentString: String
    
    init(contentString: String) {
        
        self.contentString = contentString
        
        super.init()
    }
}

    其他类中引用:
    private var myContext = 0
    
    let model: Model
    
    init(model: Model) {
        
        self.model = model
        
        self.contentString = model.contentString;
        
        super.init()
        
        self.model.addObserver(self, forKeyPath: "contentString", options: [.new, .old], context: &myContext)
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        print("modelChanged ...")
        
        if context == &myContext {
            
            if let change = change {
                
                let valueString = change[.newKey]
                
                self.contentString = valueString as? String ?? self.contentString
            }
        }else {
            
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }    

相关文章

  • swift中使用runtime修改属性值

    swift中使用runtime修改属性值 Swift中的@objc和dynamic关键字 在OC中继承自NSObj...

  • Swift中的dynamic关键字?

    //疑问1中的AnimationDelegate是一个类实例化后的对象吗?怎么大括号中还能执行语句?还是其他什么意...

  • Swift十九(OC 到 Swift 2)

    目录 是否继承 NSObject 内存分析 optional 可选协议 dynamic 关键字 Swift 代码使...

  • Core Data

    // Core Data // OC 中的 @dynamic 和 Swfit 中的 dynamic 关键字作用不同...

  • Swift:dynamic关键字

    如果有过OC的开发经验,那一定会对OC中@dynamic关键字比较熟悉,它告诉编译器不要为属性合成getter和s...

  • Swift:dynamic关键字

    如果有过OC的开发经验,那一定会对OC中@dynamic关键字比较熟悉,它告诉编译器不要为属性合成getter和s...

  • Swift dynamic关键字

    如果您有过OC的开发经验,那一定会对OC中@dynamic关键字比较熟悉,它告诉编译器不要为属性合成getter和...

  • swift之mutating关键字

    部分参考:swift之mutating关键字 swift中在structures和enumerations的方法中...

  • Swift 之关键字总结

    Swift 中有多少关键字? 在Swift官方文档的词汇结构中, 有非常多的关键字, 它们被用于声明中、语句中、表...

  • 435,Swift - mutating关键字的使用(面试点:在

    Swift中mutating关键字 Swift中protocol的功能比OC中强大很多,不仅能再class中实现,...

网友评论

      本文标题:Swift中的dynamic关键字?

      本文链接:https://www.haomeiwen.com/subject/mhxfwttx.html