ios - 无法在 ViewController 的 AppDelegate 中使用数组

标签 ios iphone swift ibeacon

在 AppDelegate 中我有下一个扩展:

extension AppDelegate: CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager!,didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
        let viewController:ViewController = window!.rootViewController as! ViewController
        viewController.beacons = beacons as! [CLBeacon]?

        NSLog("didRangeBeacons");
    }
}

在 ViewController 中我这样做:

class ViewController: UIViewController {
    var beacons: [CLBeacon]?

    override func viewDidLoad() {
        super.viewDidLoad()

        if(beacons != nil){
            println(beacons![0])
        }
    }
}

它没有给我打印任何东西。但它不是空的,是一个数组。它在日志 didRangeBeacons 中打印。那么,问题是什么以及如何访问它?

最佳答案

CLLocationManagerDelegate 的方法 locationManager(manager:,didRangeBeacons beacons:, inRegion:) 是一个异步调用。在这里,您尝试通过 viewDidLoad 方法获取 ViewController 中的 beacons 的值。

AppDelegate 上的方法可能会在 View Controller 的 viewDidLoad 执行之后触发。

检查 viewDidLoad 是否在 locationManager(manager:,didRangeBeacons beacons:, inRegion:)

之前被调用

关于ios - 无法在 ViewController 的 AppDelegate 中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30861152/

相关文章:

ios - 匹配字符串中的<a> anchor 标记,提取其 `href`

ios - 为 UINavigationBar 设置背景颜色

iphone - 在 UIButton 实例中发送额外参数

iphone - .mm 文件中的 DDMathParser

ios 13 UIContextMenu 显示缩短的 UIAction 标题

ios - 闭包中的 Inout 参数(Swift)

ios - xCode 6 调整输入大小移动ViewController

ios - fatal error : Unexpectedly found nil while unwrapping an Optional value while loading webView

iphone - ALAssetLibrary 和 iOS 5.0

swift - 如何使用 swift 为不透明度设置动画?