How to convert a swift String to CFString
Just cast it:
var str = "Hello, playground" as CFString
NSString(format: "type id: %d", CFGetTypeID(str))
Note that you'll need import Foundation
for cast as CFString
to work.
Otherwise if you only have import CoreFoundation
, you'll need to force cast as! CFString
.
How to convert String to CFMutableString?
Most immutable classes in Cocoa has a mutableCopy()
method to return a mutable clone of itself. NSString.mutableCopy()
returns an NSMutableString
. NSData.mutableCopy()
returns an NSMutableData
, etc.
You can then bridge from Cocoa (NS*
) to Foundation (CF*
) class:
let str = "Hello world"
let cfString = (str as NSString).mutableCopy() as! CFMutableString
How to convert a String type variable to CFString in Swift?
Swift.String
and NSString
bridge automatically, and NSString
and CFString
can be cast to one another, but you can't (for now?) transitively get all the way from a Swift String
to a CFString
without an extra cast or two.
Here's a Swift string, a function that takes a CFString
, and how to call it by casting to NSString
:
var str = "Hello, playground"
func takesCFString(s: CFString) {}
takesCFString(str as NSString)
Note you don't need to use CFStringRef
in Swift (and most of the Swift declarations of CFString
API don't). The compiler takes advantage of the automatic ARC bridging for CF types that was introduced with Xcode 5 to let you treat those types like Swift (or ObjC) objects.
How to cast between CFString and String with swift on linux
As far as I tried on the IBM Swift Sandbox, CFStringTransform
does not work on arbitrary CFMutableString
s. Seems it requires CFMutableString
based on UTF-16 representation.
import Foundation
#if os(Linux)
import CoreFoundation
import Glibc
#endif
public extension String {
func transformToLatinStripDiacritics() -> String{
let chars = Array(self.utf16)
let cfStr = CFStringCreateWithCharacters(nil, chars, self.utf16.count)
let str = CFStringCreateMutableCopy(nil, 0, cfStr)!
if CFStringTransform(str, nil, kCFStringTransformToLatin, false) {
if CFStringTransform(str, nil, kCFStringTransformStripDiacritics, false) {
return String(describing: str)
}
return self
}
return self
}
}
print("我在大阪住".transformToLatinStripDiacritics()) //->wo zai da ban zhu
Tested only for a few examples. So, this may not be the best solution for your issue.
CFString convert to String for kUTTypeImage in Swift 2.0 & XCODE 7
From the header file:
public var mediaTypes: [String]
// default value is an array containing kUTTypeImage.
So you can actually just delete that line.
But if you want to keep it, you just need to be explicit that you want a cast:
picker.mediaTypes = [kUTTypeImage as String]
Avoiding forced downcast from Any to CFString
Import Foundation
to give the compiler information about bridging between Core Foundation types and Foundation types. Then cast the array as you want:
import Foundation
import SystemConfiguration.CaptiveNetwork
func fetchSSIDInfo() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as? [CFString] {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
}
}
}
return ssid
}
Related Topics
Swift - How to Set Cookie in Nsmutableurlrequest
Swift: How to Access in Appdelegate Variable from the View Controller
Swift Codable Decode Manually Optional Variable
How to Work with Bindings When Using a View Model VS Using @Binding in the View Itself
Cannot Use Mutating Member on Immutable Value of Type
How to Build a Workout App on Watchos with Audio Feedback
How to Use Swift Package Manager with an Existing MACos Project
Swift Extension for Selected Class Instance
Error Handling in Swift Does Not Involve Stack Unwinding. What Does It Mean
How to Make List with Single Selection with Swiftui
Get Just the Date (No Time) from Uidatepicker
Swift Property Observer in Protocol Extension
(Swift) Nstimer Stop When Scrolling
Swift "Where" Array Extensions
How to Setup a Second Component with a Uipickerview
When Should I Use Optionals and When Should I Use Non-Optionals with Default Values