如何在Swift中将多字符数字格式转换为可读字符串?

时间:2015-08-27 22:47:09

标签: swift core-audio

我正在尝试将AudioStileBeticDescription的mFormatID属性(我使用AudioFileGetGlobalInfo)转换为可读字符串。 在Objective-C中它看起来像这样:

for (int i = 0; i < asbdCount; i++) {
  UInt32 format4cc = CFSwapInt32HostToBig(asbds[i].mFormatID);
  NSLog(@"mFormatID: %4.4s", (char*)&format4cc);
}

此代码是来自Learning Core Audio的一本CAStreamFormatTester。 asbds是一个指向AudioStreamBasicDescriptions的指针。如何将其转换为Swift?

1 个答案:

答案 0 :(得分:1)

如果asbds类型为UnsafePointer<AudioStreamBasicDescription>或类型为[AudioStreamBasicDescription],那么我认为这应该有效:

for i in 0 ..< asbdCount {
    var format4cc = CFSwapInt32HostToBig(asbds[i].mFormatID)
    withUnsafePointer(&format4cc) { cptr in
        println(String(format: "mFormatID: %4.4s", cptr))
    }
}