控制台打印到Swift 2标签

时间:2016-07-06 14:46:07

标签: ios swift label beacon

我尝试制作一个显示信标接近度的应用。我做了一个label来接收这些数据,但我不能,而且我想要显示"接近"并非所有这些数据都显示在控制台中。我尝试使用beacons[3],但该程序给了我一个错误。

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet var metrosBeacon: UILabel!
    let locationManager = CLLocationManager()
    let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647828")!, identifier: "MKT BEACONS")
    // Note: make sure you replace the keys here with your own beacons' Minor Values


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationManager.delegate = self
        if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
            locationManager.requestWhenInUseAuthorization()
        }
        locationManager.startRangingBeaconsInRegion(region)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion){
        print (beacons)
        metrosBeacon.text = "/(beacons)"

    }}

控制台数据:

  

[CLBeacon(uuid:< __ NSConcreteUUID 0x12ee586c0> FDA50693-A4E2-4FB1-AFCF-C6EB07647828,major:10004,minor:54480,proximity:1 +/- 0.05m,rssi:-32)]

谢谢你!

2 个答案:

答案 0 :(得分:2)

我们在locationManager的签名中看到beacons是一个CLBeacon对象数组:

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion)

我们看到你在数组中得到一个对象:

  

[CLBeacon(uuid:< __ NSConcreteUUID 0x12ee586c0> FDA50693-A4E2-4FB1-AFCF-C6EB07647828,major:10004,minor:54480,proximity:1 +/- 0.05m,rssi:-32)]

因此,从数组中获取第一个对象,然后从属性中获取值:

if let beacon = beacons.first {
    print(beacon.proximity)
}

当然,如果数组中有多个信标,则可以使用循环:

for beacon in beacons {
    print(beacon.proximity)
}

答案 1 :(得分:0)

CLBeaconNSObject的子类,因此有一个名为description()的方法,它返回包含对象描述的NSString

当您致电print(beacons)时,您在print类型的参数(a.k.a。[CLBeacon]或CLBeacon对象数组)上调用Array<CLBeacon >print本身并不知道如何打印CLBeacon个对象,因此会向CLBeacon询问description(),并打印出来。

输出:

  

[CLBeacon(uuid:&lt; __ NSConcreteUUID 0x12ee586c0&gt; FDA50693-A4E2-4FB1-AFCF-C6EB07647828,major:10004,minor:54480,proximity:1 +/- 0.05m,rssi:-32)]

表示单个CLBeacon object, whose description`的数组:

  

CLBeacon(uuid:&lt; __ NSConcreteUUID 0x12ee586c0&gt; FDA50693-A4E2-4FB1-AFCF-C6EB07647828,主要:10004,次要:54480,接近度:1 +/- 0.05m,rssi:-32)

为了获得距离,我们可以查看class documentation for CLBeacon,并看到它有一个proximity变量。

我们可以用以下内容打印所有邻近区域:

信标中的信标{     打印(beacon.proximity) }