如何将Swift字符串转换为CFString

44

我如何在Swift中从本地的Swift字符串或NSString创建CFString

    let path:String = NSBundle.mainBundle().pathForResource(name.stringByDeletingPathExtension, ofType:"pdf")
    let string:CFString = ??? path
    let url:CFURLRef = CFURLCreateWithFileSystemPath(allocator:kCFAllocatorDefault, filePath:string, pathStyle:CFURLPathStyle.CFURLPOSIXPathStyle, isDirectory:false)

1
在这种情况下,为什么不直接使用 let url = NSURL.fileURLWithPath(path, isDirectory:false) 呢? - David Berry
它们都已经桥接了,只需将它们转换即可。 - Sulthan
@EricD. 你是什么意思? - ciccioska
@EricD。我明白,但我认为这是一个IOS问题,不仅仅是我,我想...谢谢。 - ciccioska
5个回答

51

只需要投掷它:

var str = "Hello, playground" as CFString
NSString(format: "type id: %d", CFGetTypeID(str))
请注意,进行强制类型转换 as CFString 时,您需要使用 import Foundation
否则,如果您只使用import CoreFoundation,则需要进行强制类型转换 as! CFString

29
如果您想转换非文字字符串,您需要将其转换为NSString类型。
let replacement = "World"
let string = "Hello, \(replacement)"

let cfstring:CFString = string as NSString

Swift 知道如何将 Swift 字符串转换为 NSString 和 NSString 转换为 CFString,但似乎不知道如何在一步中完成两个步骤。


1
我希望随着 Swift 的发展,这种问题能够得到改进!从 Swift 使用 Core Foundation 是有点痛苦的... - John M. P. Knox
1.0版本仍存在问题 :( - Tristan Warner-Smith

5
今天我尝试在一个用于测试C API的游乐场中进行操作,只需import Foundation即可使"string" as CFString正常工作。

5
你可以在CFString和NSString之间进行转换,也可以在NSString和String之间进行转换。关键是当你在CFString和String之间进行转换时,必须进行双重转换。
以下是可行的示例:
var cfstr: CFString = "Why does Swift require double casting!?"
var nsstr: NSString = cfstr as NSString
var str: String = nsstr as String

这会报错“'CFString'不是 'NSString' 的子类型”:

var cfstr: CFString = "Why does Swift require double casting!?"
var str: String = cfstr as String

有什么想法为什么会这样? - Kamchatka
我不知道为什么 - 如果不需要这个要求,那会容易得多。我猜编译器还没有成熟到足以实现这一点。 - user1021430

0

如果您想将包含Swift字符串的变量转换为CFString,我认为@freytag在他的解释中已经解决了这个问题。

如果有人想看一个例子,我想我可以包括一个代码片段,在这个例子中,我将一个Swift字符串(在这种情况下是"ArialMT")强制转换为NSString,以便与Core Text的CTFontCreateWithName函数一起使用(该函数需要CFString)。 (注意:从NSString到CFString的转换是隐式的)。

    // Create Core Text font with desired size
    let coreTextFont:CTFontRef = CTFontCreateWithName("ArialMT" as NSString, 25.0, nil) 

    // Center text horizontally
    var paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.Center

    // Center text vertically
    let fontBoundingBox: CGRect = CTFontGetBoundingBox(coreTextFont)
    let frameMidpoint = CGRectGetHeight(self.frame) / 2
    let textBoundingBoxMidpoint = CGRectGetHeight(fontBoundingBox) / 2
    let verticalOffsetToCenterTextVertically = frameMidpoint - textBoundingBoxMidpoint

    // Create text with the following attributes
    let attributes = [
        NSFontAttributeName : coreTextFont,
        NSParagraphStyleAttributeName: paragraphStyle,
        kCTForegroundColorAttributeName:UIColor.whiteColor().CGColor
    ]
    var attributedString = NSMutableAttributedString(string:"TextIWantToDisplay", attributes:attributes)

    // Draw text (CTFramesetterCreateFrame requires a path).
    let textPath: CGMutablePathRef = CGPathCreateMutable()
    CGPathAddRect(textPath, nil, CGRectMake(0, verticalOffsetToCenterTextVertically, CGRectGetWidth(self.frame), CGRectGetHeight(fontBoundingBox)))
    let framesetter: CTFramesetterRef = CTFramesetterCreateWithAttributedString(attributedString)
    let frame: CTFrameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedString.length), textPath, nil)
    CTFrameDraw(frame, context)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接