美文网首页iOS Dev
swift中的Delegate

swift中的Delegate

作者: 庸者的救赎 | 来源:发表于2016-06-17 10:59 被阅读3548次

在iOS开发中,代理模式(protocol - delegate)是界面之间数据传递最为常见的一个设计模式

在Cocoa框架中也有大量使用,可以说是作为iOS开发者必备技能。

在我们OC开发时候使用delegate一般会把修饰符写成weak,这样在这个delegate实际的对象被释放的时候,会被置为nil,避免内存泄露。代码大致如下:

@property (nonatomic, weak) id<MGIndexAdReusableViewDelegate> delegate;

那么一般写过OC的童鞋去写swift,也会照本宣科,也设置为weak,没有错,但是事实情况会是怎样呢 ?看如下代码:

protocol TestDelegate {
  func testMethod()
}

class TestClass {
  weak var delegate: TestDelegate?
}

class ViewController: UIViewController, TestDelegate {
  var testInstance: TestClass!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    testInstance = TestClass()
    testInstance.delegate = self // error: 'weak' cannot be applied to non-class type 'TestDelegate'
  }
  
  func testMethod {
    print("Test!")
  }
}

上面注释部分是报错信息,为什么呢 ?

这是因为swift中的protocol是可以被除class类型以外的struct type和enum type所遵守的,那么原因不言而喻了。struct type和enum type,他们本身是不通过引用计数来管理内存的,所以也就不能使用weak来修饰,那么怎么办呢 ?

方法有两种

1.将protocol声明为Objective - C的,通过在protocol前面加@objc关键字来实现,这样就只能由class来遵守了,代码大致如下:

@objc protocol TestDelegate {
  func testMethod()
}

2.在protocol的声明后面加上class,这样可以为编译器显式的指名这个protocol只能由class来遵守,代码大致如下:

// 更推荐这种方式,易于理解,更能表现出问题的实质
protocol TestDelegate: class {
  func testMethod()
}

相关文章

  • Swift小知识

    1. 关于Swift中Protocol 1. 在 Swift 中,Delegate 就是基于 Protocol 实...

  • swift中的Delegate

    在iOS开发中,代理模式(protocol - delegate)是界面之间数据传递最为常见的一个设计模式 在Co...

  • Swift中的delegate

    // delegate // 当一个协议 没有任何的父协议时,说明它是可以被 struct, enum 实现的,声...

  • swift delegate 和 block 使用

    swift delegate 和 block 使用 delegate使用 //自定义cell 代码importUI...

  • UIApplication

    Swift 中没有了main函数,但是在app delegate中有@UIApplicationMain, 可以去...

  • delegate

    Swift的delegate 用weak修改的时候的注意事项Swift-代理

  • Swift 报 Type 'xx' does not confo

    Swift中设置完UITableView的delegate和数据源dataSource会报如下错误: 明明你该做的...

  • swift中的Delegate、Notification、Blo

    swift中的Delegate、Notification、block 的思路和实现过程和Object-C基本一致。...

  • swift delegate

  • Swift delegate

    ARC 中,对于一般的 delegate,在声明中将其指定为 weak,在这个 delegate 实际的对象被释放...

网友评论

    本文标题:swift中的Delegate

    本文链接:https://www.haomeiwen.com/subject/cnhvdttx.html