swift cfstring

Cómo convertir una cadena swift a CFString



(5)

¿Cómo puedo crear una CFString a partir de una cadena swift nativa o NSString en swift

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)


Hoy "string" as CFString hacer esto en un área de juegos para probar una API de C y solo "string" as CFString import Foundation "string" as CFString made "string" as CFString .


Lo colocas entre CFString y NSString, o entre NSString y String. El truco es que debes hacer doble lanzamiento al pasar entre CFString y String.

Esto funciona:

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

Esto da el error "''CFString'' no es un subtipo de ''NSString''":

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


Sólo tienes que lanzarlo:

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


Si desea convertir una cadena no literal, debe convertirla a NSString.

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

Swift sabe cómo convertir una cadena Swift en una NSString y una NSString en una CFString, pero parece no saber cómo hacer los dos pasos en uno.


Si estás tratando de convertir una variable que contiene una cadena Swift a una CFString, creo que @freytag la clavó con su explicación.

En caso de que alguien quisiera ver un ejemplo, pensé que incluiría un fragmento de código donde emito una cadena Swift ("ArialMT" en este caso) a una NSString para usarla con la función CTFontCreateWithName de Core Text (que requiere una CFString). (Nota: la conversión de NSString a CFString es implícita).

// 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)