【问题标题】:How to convert a swift String to CFString如何将 swift String 转换为 CFString
【发布时间】:2014-07-28 05:24:10
【问题描述】:

如何在 swift 中从原生 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)

【问题讨论】:

  • 在这种情况下,为什么不直接使用let url = NSURL.fileURLWithPath(path, isDirectory:false)
  • 它们都是桥接的,只需投射它们
  • @EricD。你什么意思?
  • @EricD。我明白,但我认为这是一个 IOS 问题,我想不仅是我……谢谢

标签: swift cfstring


【解决方案1】:

只要施放它:

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

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

【讨论】:

    【解决方案2】:

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

    let replacement = "World"
    let string = "Hello, \(replacement)"
    
    let cfstring:CFString = string as NSString
    

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

    【讨论】:

    • 我希望这类问题会随着 Swift 的发展而得到改善!使用 Swift 的 Core Foundation 很痛苦……
    • 1.0 版本仍然存在问题 :(
    【解决方案3】:

    你在 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
    

    【讨论】:

    • 知道为什么吗?
    • 我不知道为什么 - 如果不需要这样做会容易得多。我猜编译器还没有成熟到足以让这成为可能。
    【解决方案4】:

    今天我试图在操场上测试 C API,只是 import Foundation 使 "string" as CFString 工作。

    【讨论】:

      【解决方案5】:

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

      如果有人想看一个示例,我想我会包含一个代码 sn-p,在其中我将一个 Swift 字符串(在本例中为“ArialMT”)转换为一个 NSString,以便将它与 Core 的 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多