- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
作为maddy在下面的评论中提到,很明显你需要一个对象的引用才能调用它的方法。这可能实际上是我的问题:您如何跟踪实现协议(protocol)的所有对象?
回顾 Objective-C,我考虑过使用类似于 +load
或 +initialize
方法的东西,并将对象添加为特定 NSNotification 的观察者
。但这行不通,因为这些方法是类方法而不是实例方法。
所以,试着更具体一点:是否有一种方法可以在创建所有对象后调用它们?一种允许我将该对象添加到我管理的集合或作为特定 NSNotification
的观察者的方法?
P.S:我没有尝试为问题添加太多细节,因为我不想用我的糟糕、无意义的想法“污染”你。
所以...想象一下这段代码:
protocol MyProtocol: class {
func myMethod()
}
public class MyClass: MyProtocol {
func myMethod() {
print("myMethod called on MyClass")
}
}
extension UIView: MyProtocol {
func myMethod() {
print("myMethod called on UIView")
}
}
let myObject = MyClass()
let myView = UIView()
现在...我正在尝试找出一种方法,从不了解它们的第三个对象对这两个对象调用 myMethod
- 这是第三个对象的简化示例:
class MyManager {
func callMyMethodOnAllObjecs() {
// Do something here so that ALL objects present in memory that conform to MyProtocol get their myMethod called
}
}
有人吗?
最佳答案
请注意,您需要的功能属于动态类型语言,这意味着使用 Objective-C 运行时。这不会带来很多技术挑战,但会限制您可以使用的 Swift 实体的范围 - 基本上只有 NSObject
派生类。
简单地说,这是您需要的:
MyManager
中支持注册新创建的对象MyManager
的 MyProtocol
实例执行MyManager
中注册一个对象会带来管理器无限期保留该对象的风险。您可以在下面找到解决问题的代码:
// Since we need ObjectiveC specific runtime features, we need to
// restrict the protocol to the NSObject protocol
protocol MyProtocol: NSObjectProtocol {
func myMethod()
}
public class MyClass: NSObject, MyProtocol {
func myMethod() {
print("myMethod called on MyClass")
}
}
extension UIView: MyProtocol {
func myMethod() {
print("myMethod called on UIView")
}
}
extension NSObject {
// this is an alternative to the original init method, that besides the
// original edit it registers the object within MyManager
@objc func swizzledInit() -> NSObject {
// this is not recursive, as init() in exchanged with swizzledInit()
let `self` = swizzledInit()
if let `self` = self as? MyProtocol {
// the object is MyProtocol
MyManager.shared.register(self)
}
return self
}
}
class MyManager {
static let shared = MyManager()
private var objecters = [() -> MyProtocol?]()
private init() {
// let's swizzle init() with our custom init
if let m1 = class_getInstanceMethod(NSObject.self, #selector(NSObject.init)),
let m2 = class_getInstanceMethod(NSObject.self, #selector(NSObject.swizzledInit)) {
method_exchangeImplementations(m1, m2)
}
}
public func register(_ object: MyProtocol) {
// registering a block in order to be able to keep a weak reference to
// the registered object
objecters.append({ [weak object] in return object })
}
func callMyMethodOnAllObjecs() {
var newList = [() -> MyProtocol?]()
// go through the list of registered blocks, execute the method,
// and retain only the ones for wich the object is still alive
for object in objecters {
if let o = object() {
newList.append(object)
o.myMethod()
}
}
objecters = newList
}
}
// This is to make sure the manager is instantiated first,
// and thus it swizzles the NSObject initializer
_ = MyManager.shared
let myObject = MyClass()
let myView = UIView()
// an instance of MyClass and one of UIView will print stuff
MyManager.shared.callMyMethodOnAllObjecs()
综上所述,上面的代码:
NSObject
的 init
,这样除了原始的 init
之外,它还会将对象注册到您的管理器。callMyMethodOnAllObjecs
时清除其对象被释放的闭包。关于ios - 对符合协议(protocol)的所有对象调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41048058/
internal protocol Reducer { associatedtype S : BaseState associatedtype A : BaseActi
我在考虑我的应用程序中的验证检查,我认为在任何模型上调用 ValidatorFactory,实现 Validee,这意味着说哪个类负责 ValidatorCreation 听起来不错。但是下面的代码不
我已经定义了 2 个协议(protocol)。我需要第一个 (NameProtocol) 来执行 Equatable 协议(protocol)。而另一个类 (BuilderProtocol) 有一个返
在上传方面,WebDAV 协议(protocol)在哪些方面优于 HTTP 协议(protocol)。 Socket Upload 协议(protocol)和 WebDav Upload 协议(pro
是否可以在任何版本的 Swift 中扩展具有混合类/协议(protocol)类型约束的协议(protocol)?例如,仅当 Self 是 UIViewController 的子类并且符合 Protoc
我有一个协议(protocol) (ProtocolA),其中包含符合第二个协议(protocol) (ProtocolB) 的单个属性。 public protocol ProtocolA {
NSObject 协议(protocol)带有常用的协议(protocol)模板,但它似乎并不是协议(protocol)实际实现所必需的。将其排除在外似乎完全没有任何改变。那么,协议(protocol
我想根据这两种协议(protocol)的一般特征(例如开销(数据包)、安全性、信息建模和可靠性)来比较 OPC UA 和 MQTT。我在哪里可以找到每个协议(protocol)的开销和其他特性的一些示
使用 Swift 4,我正在尝试编写一个自定义协议(protocol),它提供对 @objc 协议(protocol)的一致性。 一些代码 更具体地说,我有一个自定义协议(protocol) Sear
我想定义一个在 Viper 架构中使用的协议(protocol),以使用具有弱属性的协议(protocol)在 Viper 组件之间建立连接,但我收到以下错误消息: 'weak' may only b
我在同一个网络中有 3 个 docker 容器: 存储 (golang) - 它提供了用于上传视频文件的 API。 主播 (nginx) - 它流式传输上传的文件 反向代理 (姑且称之为代理) 我有
我打算在我的项目中使用 php socket。它需要用户登录才能根据 session 填充内容。所以我的问题是,TCP/IP 协议(protocol)也像 HTTP 协议(protocol)一样为每个
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
我有一个 Swift View Controller ,它定义了一个在 Objective-C View Controller 中应该遵循的协议(protocol): ChildViewControl
我在客户那里有数百个硬件设备,需要通过telnet接口(interface)发送HTTP数据。 目标是等待数据的 Apache 2 Web 服务器和 PHP 脚本。 这已经可以正常工作了,但是我们发现
我发现如果我创建一个这样的协议(protocol): protocol MyProtocol { } 我不能这样做: weak var myVar: MyProtocol? 我找到了解决这个问题的方法
Xcode 基于模板生成了这个头文件: // this file is XYZAppDelegate.h #import @interface XYZAppDelegate : UIRespond
我在 github 中有一个公开的存储库,我正在开发一个开源应用程序,用于制作产品目录和小型 cms 内容。 我还有一个私有(private)仓库(不托管在github),它是在托管在github的开
您好,我想让别人看到私有(private) repo 代码,但不想公开我的 repo ,也不希望他们有能力更改内容。这可能吗?我查看了网站的“管理”部分,但没有找到合适的内容。谢谢大家。 最佳答案 据
我是一名优秀的程序员,十分优秀!