Swift Xcode6B3-字节交换-未定义符号__OSSwapInt16“;

Swift Xcode6B3-字节交换-未定义符号__OSSwapInt16“;,xcode,linker,swift,Xcode,Linker,Swift,我想使用Swift方法CFSwapInt16BigToHost,但我无法链接它。我针对CoreFoundation框架进行了链接,但每次都会出现以下错误: Undefined symbols for architecture i386: "__OSSwapInt16", referenced from: 我错过了什么吗?看起来这些都是通过宏和内联函数的组合来处理的,所以。。。我不知道为什么它不会被静态编译成CF版本: 一般来说,要解决这种依赖性之谜,您只需搜索不带前缀下划线的裸函数名,然后

我想使用Swift方法
CFSwapInt16BigToHost
,但我无法链接它。我针对CoreFoundation框架进行了链接,但每次都会出现以下错误:

Undefined symbols for architecture i386:
  "__OSSwapInt16", referenced from:

我错过了什么吗?

看起来这些都是通过宏和内联函数的组合来处理的,所以。。。我不知道为什么它不会被静态编译成CF版本:

一般来说,要解决这种依赖性之谜,您只需搜索不带前缀下划线的裸函数名,然后找出它应该从何处链接

#define OSSwapInt16(x)  __DARWIN_OSSwapInt16(x)
然后

然后

\uuuuu DARWIN\uOS\uInline
__uint16\u t
_OSSwapInt16(
__uint16\u t\u数据
)
{
返回(((数据>8));
}
我知道这不是一个真正的答案,但它太大了,无法发表评论,
我认为您可能需要找出swift导入标题的方式是否存在问题。。。例如,如果宏在swift设置中导入标题不正确。

是的,由于某些原因,
CFSwap…
函数不能在swift程序中使用

但是自从Xcode 6 beta 3以来,所有整数类型都有
little/bigEndian:
构造函数 和
little/bigEndian
属性。 从
UInt16
struct定义:

/// Creates an integer from its big-endian representation, changing the
/// byte order if necessary.
init(bigEndian value: UInt16)

/// Creates an integer from its little-endian representation, changing the
/// byte order if necessary.
init(littleEndian value: UInt16)

/// Returns the big-endian representation of the integer, changing the
/// byte order if necessary.
var bigEndian: UInt16 { get }

/// Returns the little-endian representation of the integer, changing the
/// byte order if necessary.
var littleEndian: UInt16 { get }
例如:

// Data buffer containing the number 1 in 16-bit, big-endian order:
var bytes : [UInt8] = [ 0x00, 0x01]
let data = NSData(bytes: &bytes, length: bytes.count)

// Read data buffer into integer variable:
var i16be : UInt16 = 0
data.getBytes(&i16be, length: sizeofValue(i16be))
println(i16be) // Output: 256

// Convert from big-endian to host byte-order:
let i16 = UInt16(bigEndian: i16be)
println(i16) // Output: 1

更新:从Xcode 6.1.1开始,
CFSwap…
功能在Swift中可用,因此

let i16 = CFSwapInt16BigToHost(bigEndian: i16be)
let i16 = UInt16(bigEndian: i16be)

这两种方法都有效,结果相同。

正如其他答案所指出的那样,_OSSwapInt16交换方法似乎不在CFByte框架的swift标题中。我认为类似swift的备选方案是:

var dataLength: UInt16 = 24
var swapped = UInt16(dataLength).byteSwapped 

在我的帖子中,有一个精确的错误。我想把字节从bigendian交换到主机@yageek:
让x=UInt16(bigEndian:y)
执行与
x=cfswapint16bigtoost(y)
相同的操作。这就是你要找的吗?
let i16 = CFSwapInt16BigToHost(bigEndian: i16be)
let i16 = UInt16(bigEndian: i16be)
var dataLength: UInt16 = 24
var swapped = UInt16(dataLength).byteSwapped