2

The question is very simple. Just how to convert swift String to CFMutableString. I only found How to convert a swift String to CFString. Any help will be appreciated.

Community
  • 1
  • 1
Lumialxk
  • 6,239
  • 6
  • 24
  • 47

3 Answers3

2

as CFMutableString is toll-free bridged from NSMutableString, you can just create NSMutableString from your String then cast it to CFMutableString:

let str: String = "test 123"
let cfmStr = NSMutableString(string: str) as CFMutableString
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
2

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
Code Different
  • 90,614
  • 16
  • 144
  • 163
0

Using the test string let str: String = "test 123"

For CFMutableString do:

(str as! CFMutableString)

For CFString you can do:

let cfStr: CFString = str

or

(str as CFString)

since the cast always succeeds.

Daniel
  • 20,420
  • 10
  • 92
  • 149