【问题标题】:Swift using delegate written in Objective-CSwift 使用用 Objective-C 编写的委托
【发布时间】:2014-07-11 07:09:38
【问题描述】:

我在一个 swift 文件中使用了一个带有委托方法的定制类,这些委托方法是用 Objective-C 编写的。桥接头已设置,因此我可以在 .swift 文件中引用 .m 和 .h 文件中的类。这里有一个问题,我试图将委托方法包含到 swift 文件中,它显示消息 Type 'YourClassName' does not conform to protocol 'YourDelegateWrittenInObjC '。当我在错误消息上Command + Left Click时,委托方法上会出现一些带有灰色背景的消息。

希望有人可以帮助我解决这个问题。提供打印屏幕以帮助调试:

已编辑(包括委托声明):

给你:

@protocol CollapseClickDelegate
@required
-(int)numberOfCellsForCollapseClick;
-(NSString *)titleForCollapseClickAtIndex:(int)index;
-(NSString *)leftTitleForCollapseClickAtIndex:(int)index;
-(NSString *)rightTitleForCollapseClickAtIndex:(int)index;
-(UIView *)viewForCollapseClickContentViewAtIndex:(int)index;

@optional
-(UIColor *)colorForCollapseClickTitleViewAtIndex:(int)index;
-(UIColor *)colorForTitleLabelAtIndex:(int)index;
-(UIColor *)colorForTitleArrowAtIndex:(int)index;
-(void)didClickCollapseClickCellAtIndex:(int)index isNowOpen:(BOOL)open;

@end

【问题讨论】:

  • 显然是在抱怨类型。委托方法中声明的类型与 objc 类所需的类型不同。请发布 objc 协议声明。
  • 我已添加到问题中。

标签: delegates swift ios8


【解决方案1】:

Swift 原始类型(如Int)在与Objective C 交互时会自动桥接到NSNumber。

当你在 Swift 端写 Int 时,你必须在 Objective C 端有一个 NSNumber

由于您的 objc 协议使用 int(c 原始类型)而不是 NSNumber 作为某些参数/返回值的类型,因此您应该将其与正确的类型桥接,即 CInt

在 Swift 端将您所有的 Int 更改为 CInt,一切都会正常工作

您可以阅读更多here

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多