- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,这是一个大问题。我有一个用 ObjC 编写的库( this )。在那里我们有一个明确的协议(protocol)。当我尝试在 swift 文件中使用它时,我不断得到:
Type "XXX" does not conform to protocol "XXX"
为了简化事情,我创建了一个测试项目 - 它应该创建为 Swift 项目。
然后创建 ObjC 头文件(我称之为 StupidProtocol.h),其中包含以下协议(protocol)(请注意每个名称和值与给定的完全匹配,包括大写/小写):
@protocol MyProtocol <NSObject>
- (NSString *)getAxisLabel:(id)axis Value:(CGFloat)value;
@end
在桥接头中:
#import "StupidProtocol.h"
然后返回 Swift 文件:
class ViewController: UIViewController, MyProtocol
{
func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! {
return ""
}
}
尽管自动完成功能为我完成了 getAxisLabel 函数,但是我们再次遇到了这个错误。
我强烈怀疑问题出在参数“value”及其函数参数“Value”上。
任何帮助将不胜感激。
编辑
请注意 this library从技术上讲不是我的,所以我无法更改它。我需要一种在 Swift 中使用它而不改变它的原始声明的方法。我的例子只是为了简化我的问题。
我尝试了什么
我尝试过以下场景但没有成功:
“参数名称与协议(protocol)所需的参数名称不同”错误
func getAxisLabel(axis: AnyObject!, Value value: CGFloat) -> String! {
return ""
}
“参数名称与协议(protocol)所需的参数名称不同”错误
func getAxisLabel(axis: AnyObject!, Value: CGFloat) -> String! {
return ""
}
“类型不符合协议(protocol)”
func getAxisLabel(axis: AnyObject!, Value: CGFloat) -> NSString! {
return ""
}
解决方案
最佳答案
我不知道这是否是一个错误。 Objective-C协议(protocol)方法
- (NSString *)getAxisLabel:(id)axis Value:(CGFloat)value;
(Value:
中带有大写“V”)映射到 Swift 为
func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String!
(value:
中包含小写“v”),但没有
func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! { }
func getAxisLabel(axis: AnyObject!, Value: CGFloat) -> String! { }
被编译器接受以满足协议(protocol)要求。
作为解决方法,您可以使用显式注释该方法Objective-C 选择器名称:
class ViewController: UIViewController, MyProtocol
{
@objc(getAxisLabel:Value:)
func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! {
return ""
}
}
关于ios - ObjC 协议(protocol)在 Swift 中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37964323/
我正在尝试在@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 中表示” 我假设问题出在“错误”参数上,但不
我是一名优秀的程序员,十分优秀!