【发布时间】:2014-11-08 20:00:35
【问题描述】:
我在 Objective-C 中有以下代码:
- (double)readDouble
{
double value = 0.0;
if ([self read:(uint8_t *)&value maxLength:8] != 8)
{
NSLog(@"***** Couldn't read double");
}
return value;
}
它有效。但我不知道如何将其转换为 Swift。这是我的代码:
public func readDouble() -> Double {
var value : Double = 0.0
var num = self.read((uint8_t *)&value, maxLength:8) // got compiling error here!
if num != 8 {
}
}
错误信息是:
不能使用类型为 '($T4, maxLength: IntegerLiteralConvertible)'
有人可以帮忙吗?谢谢
我正在使用的测试数据(1.25):
14 AE 47 E1 7A 14 F4 3F
更新:
一个简单的 c 解决方案,但如何在 Swift 中做到这一点?
double d = 0;
unsigned char buf[sizeof d] = {0};
memcpy(&d, buf, sizeof d);
【问题讨论】:
标签: ios swift nsinputstream