import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let p = Person(); p.name = "jack"; p.age = 11; p.say(); } }
@_silgen_name("sum") func swift_sum(a: Int, b: Int) -> Int
@objcMembers class Car: NSObject { var speed: Float; var name: String; init(speed: Float, name: String) { self.speed = speed; self.name = name; } func run() -> Void { print("出发,时速:\(self.speed)") } } extension Car { func circleRun() { print("跑圈,时速:\(self.speed)") } }
@objcMembers class Car: NSObject { var speed: Float; @objc(bigName) //重写暴露给OC的方法 var name: String; init(speed: Float, name: String) { self.speed = speed; self.name = name; } func run() -> Void { print("出发,时速:\(self.speed)") } func runPerform() -> Void { perform(#selector(run)) } } extension Car { @objc(bigRun) func circleRun() { print("跑圈,时速:\(self.speed)") } }
@objc protocol RunProtocol {}
class Computer: NSObject { @objc dynamic func work(num: Int) { print("工作时长:\(num)") } }
class Person: NSObject { @objc dynamic var age: Int = 10 override init() { super.init() self.addObserver(self, forKeyPath: "age", options: .new, context: nil) } override class func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { print("observer回调:\(change!)") } deinit { self.removeObserver(self, forKeyPath: "age") } }
@objcMembers class Car: NSObject { var speed: Float; @objc(bigName) //重写暴露给OC的方法 var name: String; init(speed: Float, name: String) { self.speed = speed; self.name = name; } dynamic func fillOil() -> Void { print("加油") } }