41

如何从本机 swift String 或 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)
4

5 回答 5

47

只需投射它:

var str = "Hello, playground" as CFString
NSString(format: "type id: %d", CFGetTypeID(str))

请注意,您需要import Foundation演员as CFString才能工作。
否则,如果您只有import CoreFoundation,则需要强制 cast as! CFString

于 2014-06-06T21:14:55.440 回答
29

如果要转换非文字字符串,则必须将其强制转换为 NSString。

let replacement = "World"
let string = "Hello, \(replacement)"

let cfstring:CFString = string as NSString

Swift 知道如何将 swift 字符串转换为 NSString 并将 NSString 转换为 CFString,但似乎不知道如何将这两个步骤合二为一。

于 2014-07-18T07:55:26.543 回答
4

你可以在 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
于 2014-07-23T00:52:56.983 回答
4

今天我试图在操场上做这个来测试一个 C API 并且刚刚import Foundation成功"string" as CFString

于 2016-01-09T20:33:52.047 回答
0

如果您尝试将包含 Swift 字符串的变量转换为 CFString,我认为@freytag 用他的解释将它钉牢了。

如果有人想看一个例子,我想我会包含一个代码片段,我将一个 Swift 字符串(在本例中为“ArialMT”)转换为一个 NSString,以便将它与 Core Text 中的 CTFontCreateWithName 函数一起使用(这需要CF 字符串)。(注意:从 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)
于 2014-08-07T22:27:22.053 回答