本教程的最后一部分建议我将以下代码添加到视图控制器
if(beacons != nil) {
return beacons!.count
} else {
return 0
}
这样做我在邮件标题中发布了错误。似乎(根据我有限的知识和研究),因为信标变量未被声明为可选项?这可能是我对错误地读取类似堆栈溢出帖子的解释。
请有人解释我将如何解决这个问题?我的两个类(app委托和视图控制器如下)
extension AppDelegate: CLLocationManagerDelegate {
func sendLocalNotificationWithMessage(message: String!) {
let notification:UILocalNotification = UILocalNotification()
notification.alertBody = message
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func locationManager(manager: CLLocationManager!,
didRangeBeacons beacons: [AnyObject],
inRegion region: CLBeaconRegion!) {
NSLog("didRangeBeacons");
let viewController:ViewController = window!.rootViewController as! ViewController
viewController.beacons = beacons as! [CLBeacon]
viewController.tableView!.reloadData()
var message:String = ""
if(beacons.count > 0) {
let nearestBeacon:CLBeacon = beacons[0] as! CLBeacon
if(nearestBeacon.proximity == lastProximity ||
nearestBeacon.proximity == CLProximity.Unknown) {
return;
}
lastProximity = nearestBeacon.proximity;
switch nearestBeacon.proximity {
case CLProximity.Far:
message = "You are far away from the beacon"
case CLProximity.Near:
message = "You are near the beacon"
case CLProximity.Immediate:
message = "You are in the immediate proximity of the beacon"
case CLProximity.Unknown:
return
}
} else {
message = "No beacons are nearby"
}
NSLog("%@", message)
sendLocalNotificationWithMessage(message)
}
func locationManager(manager: CLLocationManager!,
didEnterRegion region: CLRegion!) {
manager.startRangingBeaconsInRegion(region as! CLBeaconRegion)
manager.startUpdatingLocation()
NSLog("You entered the region")
sendLocalNotificationWithMessage("You entered the region")
}
func locationManager(manager: CLLocationManager!,
didExitRegion region: CLRegion!) {
manager.stopRangingBeaconsInRegion(region as! CLBeaconRegion)
manager.stopUpdatingLocation()
NSLog("You exited the region")
sendLocalNotificationWithMessage("You exited the region")
}
}
查看控制器
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView?
var beacons: [CLBeacon] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(beacons != nil) {
return beacons!.count
} else {
return 0
}
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell? =
tableView.dequeueReusableCellWithIdentifier("MyIdentifier") as? UITableViewCell
if(cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: "MyIdentifier")
cell!.selectionStyle = UITableViewCellSelectionStyle.None
}
let beacon:CLBeacon = beacons[indexPath.row]
var proximityLabel:String! = ""
switch beacon.proximity {
case CLProximity.Far:
proximityLabel = "Far"
case CLProximity.Near:
proximityLabel = "Near"
case CLProximity.Immediate:
proximityLabel = "Immediate"
case CLProximity.Unknown:
proximityLabel = "Unknown"
}
cell!.textLabel!.text = proximityLabel
let detailLabel:String = "Major: \(beacon.major.integerValue), " +
"Minor: \(beacon.minor.integerValue), " +
"RSSI: \(beacon.rssi as Int), " +
"UUID: \(beacon.proximityUUID.UUIDString)"
cell!.detailTextLabel!.text = detailLabel
return cell!
}
}
extension ViewController: UITableViewDelegate {
}
答案 0 :(得分:0)
您的代码beacons
似乎不是可选值。您可以删除整个支票,然后再转发。