gpt4 book ai didi

ios - 找不到 != 的重载 - Swift iBeacon

转载 作者:行者123 更新时间:2023-11-28 09:03:13 26 4
gpt4 key购买 nike

我正在学习可以在这里找到的教程 http://ibeaconmodules.us/blogs/news/14702963-tutorial-swift-based-ibeacon-app-development-with-corelocation-on-apple-ios-7-8

教程的最后一部分建议我将以下代码添加到 View Controller

if(beacons != nil) {
return beacons!.count
} else {
return 0
}

在这样做的过程中,我收到了消息标题中发布的错误。看来(根据我有限的知识和研究)这是因为 beacons 变量没有声明为可选的?这可能是我错误地阅读了类似的堆栈溢出帖子。

有人可以解释一下我将如何解决这个问题吗?我的两个类(app delegate和view controller如下)

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")
}
}

View Controller

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 {

}

最佳答案

在您的代码中,beacons 似乎不是可选值。您可以删除整个支票,然后再向前看。

关于ios - 找不到 != 的重载 - Swift iBeacon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31574602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com