我正在尝试获取advertisementData[CBAdvertisementDataManufacturerDataKey]
的值以对其进行操作。我该怎么办?
我已经尝试过在该论坛上找到一些解决方案,例如:Convert NSInlineData to Nstring in ios,但这是在Objective-C中。我已将其转换为快捷方式,但没有用
let manufacturerdata = advertisementData["kCBAdvDataManufacturerData"] as! NSData
print("value : \(manufacturerdata)")
let manufacturerdatastr = String(data: manufacturerdata as Data, encoding: .utf8)
print("data string : \(manufacturerdatastr)")
我期望值为manufacturerdata
,但实际输出为nil
答案 0 :(得分:0)
我已经找到解决问题的方法:
adData = advertisementData[CBAdvertisementDataManufacturerDataKey]
let data : NSData = adData as! NSData
var str1 : String = data.description
str1 = str1.replacingOccurrences(of: "<", with: "")
str1 = str1.replacingOccurrences(of: ">", with: "")
if let value = Int(str1, radix: 16) {
print(value)
DataInt=value
}
因此,我可以在变量DataInt中获取类型为int且十进制为int的AdvertiseData [CBAdvertisementDataManufacturerDataKey]的值。
希望对您有帮助