在Linux上桥接到CoreFoundation或从CoreFoundation桥接

前端之家收集整理的这篇文章主要介绍了在Linux上桥接到CoreFoundation或从CoreFoundation桥接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在使用CoreFoundation和Foundation的 Linux上编译一些代码,但 Linux并没有像macOS和iOS那样实​​现桥接.

Objective-C和Swift工作之间的桥梁:

import Foundation
import CoreFoundation
import Glibc

func wantsNSString(_ string: NSString) {
        print(string)
}

let string = "Hello,world!"
wantsNSString(string._bridgeToObjectiveC())

但我无法弄清楚如何桥接到CoreFoundation.我不能只将NSString传递给想要CFString的函数

import Foundation
import CoreFoundation
import Glibc

func wantsCFString(_ string: CFString) {
        print(string)
}

let string = "Hello,world!"
wantsCFString(string._bridgeToObjectiveC()) //error: cannot convert value of type 'String._ObjectType' (aka 'NSString') to expected argument type 'CFString'

我不能像在macOS上一样:

import Foundation
import CoreFoundation
import Glibc

func wantsCFString(_ string: CFString) {
        print(string)
}

let string = "Hello,world!"
wantsCFString(string._bridgeToObjectiveC() as CFString)
//error: 'String._ObjectType' (aka 'NSString') is not convertible to 'CFString'; did you mean to use 'as!' to force downcast?

用作!像错误消息建议编译但导致运行时崩溃(非法指令),并作为?产生错误

error: conditional downcast to CoreFoundation type 'CFString' will always succeed

Bridging.swift具有用于在NS和CF类型之间进行转换的协议,并且许多类型具有初始化器和属性,但这些都是内部或私有的.我可以使用CFStringCreateWithCString,但这需要与其他类对一起工作,如InputStream和CFReadStream.

我在这里遗漏了什么,或者在Linux上真的没有办法在Foundation和CoreFoundation类型之间进行转换?

解决方法

@H_404_25@ 看起来我可以将unsafeBitCast NSString发送到CFString:
import Foundation
import CoreFoundation
import Glibc

func wantsCFString(_ string: CFString) {
        print(string)
}

let string = "Hello,world!"
wantsCFString(unsafeBitCast(string._bridgeToObjectiveC(),to: CFString.self)
//prints "Hello,world!"

这是有道理的,因为CoreFoundation和Foundation类型被设计为具有相同的内存布局 – 这就是免费桥接工作的原因.我有点惊讶它虽然适用于Swift的Foundation实现.

原文链接:https://www.f2er.com/linux/394952.html

猜你在找的Linux相关文章