Ios CLPeripheralManager.StartAvertising不接受CLBeaconRegion.peripheralDataWithMeasuredPower的返回值
根据forIos CLPeripheralManager.StartAvertising不接受CLBeaconRegion.peripheralDataWithMeasuredPower的返回值,ios,swift2,ibeacon,cbperipheralmanager,clbeaconregion,Ios,Swift2,Ibeacon,Cbperipheralmanager,Clbeaconregion,根据forCLBeaconRegion,仍然可以将peripheralDataWithMeasuredPower:方法的输出传递给clperipheraldmanager的开始转换:方法 获取信标广告数据 -测量功率的外围设备数据: 检索可用于将当前设备作为信标播发的数据 声明 SWIFT func peripheralDataWithMeasuredPower(\u measuredPower:NSNumber?)->NSMutableDictionary 目标-C -(NSMutableDi
CLBeaconRegion
,仍然可以将peripheralDataWithMeasuredPower:
方法的输出传递给clperipheraldmanager
的开始转换:
方法
获取信标广告数据
-测量功率的外围设备数据:
检索可用于将当前设备作为信标播发的数据
声明
SWIFT
func peripheralDataWithMeasuredPower(\u measuredPower:NSNumber?)->NSMutableDictionary
目标-C
-(NSMutableDictionary*\u Nonnull)具有测量功率的外围设备数据:(NSNumber*\u Nullable)测量功率
参数
measuredPower
设备的接收信号强度指示器(RSSI)值(以分贝为单位)。
该值表示从一个角度测量的信标强度
在测距过程中使用。指定nil
以使用默认值
设备的值
返回值
一个你可以使用的数据字典
与cbperipheraldmanager
一起使用,以公布当前
作为信标的装置
讨论
返回的字典对信标的标识进行编码
信息以及广告所需的其他信息
信标您不需要访问字典内容
直接的。将字典传递给
cbperipheraldmanager
开始宣传信标
可用性
在iOS 7.0及更高版本中提供
但是,peripheralDataWithMeasuredPower:
返回一个NSMutableDictionary
,而clperipheraldanager
的开始转换:
方法接受一个[String:AnyObject]?
的快速字典,尽管文档声称它接受一个NSDictionary
。以下代码适用于Swift 1.0:
// Set up a beacon region with the UUID, Major and Minor values
let region = CLBeaconRegion(proximityUUID:beaconUUID!, major:withMajor.unsignedShortValue, minor:withMinor.unsignedShortValue, identifier:"com.example.beacon")
// Attempt to set up a peripheral with the measured power
let peripheralData = region.peripheralDataWithMeasuredPower(withPower)
_peripheralManager!.startAdvertising(peripheralData)
在Swift 2.0中,相同的代码无法编译,并带有警告:
未能编译,并发出警告:
NSDictionary
不能转换为[字符串:AnyObject]
;是吗
意味着使用作为代码>强制向下播放
然而,强迫一次沮丧总是失败的
这是一个文档错误,Swift 2.0中的错误,还是我遗漏了什么?问题似乎是NSMutableDictionary
不容易转换为Swift的Dictionary
,但nsdictionary
是。因此,我首先将NSMutableDictionary
转换为nsdictionary
:
let pd = NSDictionary(dictionary: region.peripheralDataWithMeasuredPower(nil))
as! [String: AnyObject]
peripheralManager.startAdvertising(pd)
而且它有效 希望有比我更擅长Swift的人能给出答案。我看到的和你一样!我的解决方案是强制转换到字典,然后在函数调用中进行可选强制转换让peripheralData=region.peripheralDataWithMeasuredPower(withPower)作为字典_外围设备管理器!。开始转换(外设数据为?[String:AnyObject])
。是的,这也适用于我。关键是将作为字典
添加到外围设备数据
分配中。没有它,你就不能做沮丧的事。我很想看到有人提供一个更优雅的答案,但我没有。