- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习可以在这里找到的教程 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/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!