【问题标题】:How to use Reachability Objective-C class in Swift如何在 Swift 中使用可达性 Objective-C 类
【发布时间】:2015-04-08 08:31:20
【问题描述】:

在应用使用期间连接发生变化时,我需要得到通知。我已将 Reachability.h .m 文件导入到我的 Swift 项目中,并通过添加 #include "Reachability.h" 来桥接它。

在 Apple 的可达性示例 project 中,设置了一个观察者,以便在连接更改时收到通知。如何在 Swift 中进行设置?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

在 Swift 中没有等效的观察者。 Swift 有像 willSetdidSet 这样的属性观察者。

【问题讨论】:

  • 我不明白你的问题
  • 你在 Objective-C 中使用过 Reachability.h .m 吗?
  • 是的,我在目标 c 中使用过它
  • 如何在 Swift 中使用ReachabilityaddObserver line is Swift 你怎么写?
  • 好的,我给你那行

标签: ios objective-c swift network-programming


【解决方案1】:

请参考以下链接以了解 swift https://github.com/ashleymills/Reachability.swift 的可达性

对于您的添加观察者,请像这样使用它

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多