gpt4 book ai didi

xcode - Swift Xcode6B3 - 字节交换 - undefined symbol "__OSSwapInt16"

转载 作者:可可西里 更新时间:2023-10-31 23:57:19 31 4
gpt4 key购买 nike

我想使用 Swift 方法 CFSwapInt16BigToHost 但我无法链接它。我链接到 CoreFoundation 框架,但每次出现以下错误:

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

我错过了什么吗?

最佳答案

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

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

/// 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)

两者都有效,结果相同。

关于xcode - Swift Xcode6B3 - 字节交换 - undefined symbol "__OSSwapInt16",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24866092/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com