简体   繁体   English

控制台打印以使用Swift 2进行标签

[英]Console print to label with Swift 2

I try to make an app that shows the proximity of beacons. 我尝试制作一个显示信标接近度的应用程序。 I made a label to receive this data, but I can't, and i want to just show "proximity" not all this data that shows in console. 我做了一个label来接收这些数据,但是我不能,我只想显示“接近”,而不是显示控制台中显示的所有这些数据。 I try with beacons[3] but the program gives me a error. 我尝试使用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)"

    }}

Console data : 控制台数据:

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

Thanks Guys!! 多谢你们!!

We see in locationManager 's signature that beacons is an array of CLBeacon objects: 我们在locationManager的签名中看到, beacons是CLBeacon对象的数组:

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

And we see that you get one object in the array: 我们看到您在数组中得到一个对象:

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

So, get this first object from the array, then get the value from the property: 因此,从数组中获取第一个对象,然后从属性中获取值:

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

Of course, if you have several beacons in the array, you may use a loop: 当然,如果阵列中有多个信标,则可以使用循环:

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

CLBeacon is a subclass of NSObject , thus is has a method called description() , which returns an NSString containining the description of the object. CLBeaconNSObject的子类,因此有一个称为description()的方法,该方法返回包含对象描述的NSString

When you call print(beacons) , you're calling print on an argument of type [CLBeacon] (aka Array<CLBeacon > , or an Array of CLBeacon objects.). 当你调用print(beacons) ,要调用print上类型的参数[CLBeacon]又名Array<CLBeacon >或CLBeacon对象Array)。 print doesn't itself know how to print CLBeacon objects, so it asks the CLBeacon for its description() , and prints that. print本身并不知道如何打印CLBeacon对象,因此它向CLBeacon询问其description()并进行打印。

The output: 输出:

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

represents an array of a single CLBeacon object, whose description` is: 表示单个CLBeacon object, whose的数组CLBeacon object, whose描述为:

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

To get just the proximity, we can look into the class documentation for CLBeacon , and see that it has a proximity variable we can get. 要获取接近度,我们可以查看CLBeacon类文档 ,并查看它具有可以获取的proximity变量。

We can print all proximity with something like: 我们可以使用以下方式打印所有接近度:

for beacon in beacons { print(beacon.proximity) } 用于信标中的信标{print(beacon.proximity)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM