- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试使用条件扩展创建 MKMapViewDelegate 的默认实现,如下所示:
extension MKMapViewDelegate where Self: NSObject {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
...
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
}
但是当我编译代码时,我收到了警告
Non-'@objc' method 'mapView(_:viewFor:)' does not satisfy optional requirement of '@objc' protocol 'MKMapViewDelegate'
我预计“Self”与 NSObject 的一致性意味着警告不会发生。除了警告之外,即使委托(delegate)实例是 UIViewController 并因此符合 NSObject,也不会调用委托(delegate)方法。
我是否误解了“where”在扩展程序中的工作方式?
最佳答案
NSObject
由于接受了 Doug Gregor's proposal: SE-0160
.,自 Swift 4 起将不再推断 @objc
我承认我没有花时间去了解你的错误背后的原因。我只是发布了这个答案,希望阅读您的问题的任何其他人都能看到以下建议。
为整个模块的协议(protocol)提供默认实现是不好的做法或“代码味道”。我会推荐一种替代方法,其中您创建一个自定义类型,例如 MapViewDelegate
,它具有一些默认行为,可以在明确符合该 protocol
的类型之间共享。
例如:
import MapKit
protocol MapViewDelegate: MKMapViewDelegate {}
extension MapViewDelegate where Self: NSObject {
func annotationView(for annotation: MKAnnotation, in mapView: MKMapView) -> MKAnnotationView? {
…
}
func renderer(for overlay: MKOverlay, in mapView: MKMapView) -> MKOverlayRenderer {
…
}
}
final class MyMapDelegate: NSObject, MapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return annotationView(for: annotation, in: mapView)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
return renderer(for: overlay, in: mapView)
}
}
关于swift - 非 -'@objc' 方法不满足带有条件扩展的 '@objc' 协议(protocol)的可选要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39118030/
我正在尝试在@objc 协议(protocol)中创建一个可选函数,文档类型也是@objc 枚举。但是我收到了这个错误: Method cannot be marked @objc because t
我有一个像这样定义的 objective-c API: - (instancetype)initWithItems:(NSArray *)items reuseIdentifier:(NSString
我有一个 objc 程序,我想使用一个用 objc++ 编写的小部件(即 https://launchpad.net/scintilla-cocoa )。我该怎么做?基本上我想要一个新的窗口 Cont
默认情况下,当您请求一个新的 ObjC 类时,Xcode 会同时创建一个 .h 和一个 .m 文件。一切正常,直到您需要在项目的其他地方引用任何 C++ 文件并开始#import 将其导入 .h 或
我正在尝试在我的 objective-c 项目中添加 swift 库 https://github.com/vimeo/VimeoUpload 我已经在我的项目中添加了 #import “-Swift
我的项目名称是ObjCSwift。创建项目后,我使用 xib 创建一个新的 swift 文件 SecondViewController.swift。 我已在 AppDelegate.m 中导入了 #i
同时 here Apple 声明 Swift available 标志也应该应用于 objc,它对我不起作用。我做错了什么? 我在 Swift 文件中有以下声明: @objc protocol Rea
我创建了一个 echo 命令的克隆,这比我想象的要复杂。这是我的问题:char *argv[] 是如何工作的?我知道 char myString[] 是如何工作的,但没有那种创建字符串的奇怪方法: 是
有什么区别: @objc class MyClass: NSObject{} 和 @objc(MyClass) class MyClass: NSObject{} 最佳答案 @objc 修饰符在 Sw
考虑: class SomeCppClass { public: SomeCppClass() {} ; ~SomeCppClass() {} ; } ; @interface Tes
我目前正在 UIButton 类别中使用 block 。但是我遇到了这个无法解决的错误: error: /usr/include/objc/objc-class.h: No such file or
我正在 Objective C 中构建一个动态框架,我已经使用 Swift 单元测试目标进行了单元测试。到目前为止,这一直运行良好。 我现在需要将我正在构建的框架与另一个 obj-c 动态框架链接起来
我经常会开始一个小的“辅助类”,发现我可以用 C 函数完全满足我的需求(尽管这些函数可能经常在其中包含 ObjC,可能返回类型或 NSDictionary* 的一些参数或其他什么..),所以我将删除@
在 .mm 文件中混合使用 Objective C 和 C++ 时,是否存在任何陷阱,我是否应该尽可能坚持使用纯 ObjC? 最佳答案 除了 Objective-C 编程指南的 Objective-C
这个问题在这里已经有了答案: Non-'@objc' method does not satisfy optional requirement of '@objc' protocol (3 个答案)
我正在尝试使用条件扩展创建 MKMapViewDelegate 的默认实现,如下所示: extension MKMapViewDelegate where Self: NSObject {
概述: 我有一个协议(protocol) P1,它提供了一个 Objective-C 可选函数的默认实现。 当我提供可选函数的默认实现时,会出现警告 编译器警告: Non-'@objc' method
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我有一个情况。如果有人对此有解决方案,我将不胜感激 我有一个 objC enum 说 Abc 我在一个 swift 类中将其声明为 MySwiftClass.swift var abc : Abc!
我试图从 Objective-C 类调用此函数,但遇到了问题。我收到错误消息“方法无法标记为@objc,因为参数 3 的类型无法在 Objective-C 中表示” 我假设问题出在“错误”参数上,但不
我是一名优秀的程序员,十分优秀!