Swift: Convert Unmanaged to String

Edit 1 (20th October 2014): Apple has now fixed this issue. To convert an unmanaged object to managed, just use the takeUnretainedValue() or the takeRetainedValue() method on it, based on whether you want to take a retained or unretained value.

So let’s say you have an Unmanaged<AnyObject> value that you know internally contains a value of type CFStringRef and you want to convert this to a value of type String in Swift. This is how I have managed to do that:

func convertCfTypeToString(cfValue: Unmanaged!) -> String?{

/* Coded by Vandad Nahavandipoor */

let value = Unmanaged.fromOpaque(
cfValue.toOpaque()).takeUnretainedValue() as CFStringRef
if CFGetTypeID(value) == CFStringGetTypeID(){
return value as String
} else {
return nil
}
}

Shown in Xcode it looks like this:

Screen Shot 2014-07-07 at 14.43.52

Creating string enumerations in Objective-C (The ultimate solution)

A while ago I wrote on my blog about a solution to one of the most common questions asked by Objective-C programmers which is “How can I create string enumerations?”. Well, the solution that I’ve given has immediately become one of the top subjects that attracts developers to my blog, as I can see in my stats. I thought I should now take it to a whole other level and get rid of the limitations that I had presented in the old solution, and come up with a fresh perspective.

The following video is the result of my work on this subject. I hope you’ll enjoy watching it.

String enums in Objective-C

Note: I have written a newer and better solution to this problem in a new video, which you can watch by clicking here.

Sooo a lot of programmers think enumeration items cannot be strings, and they are right, BUT, there is a BUT. You have to understand that C strings that are made out of 4 characters, each of which is 1 byte long, constitute a memory address that is 4 bytes long, or just the equivalent of int, NSInteger or whatever you want to call it.

So if you put a value such as ‘Good’ for the enumeration item, in fact, the compiler will translate the values of ‘G’, ‘o’, ‘o’ and ‘d’ as their numerical values and will generate a hexadecimal value and put it as the integral value of the enumeration item.

Let’s say we want to say “Good”, or “Nice” all in enumeration items in C. Here is the example, this code runs fine in Objective-C for iOS or OS X as well:

Screen Shot 2013-03-24 at 19.30.24

The output of this is the string “dooG” printed to the screen if you run this on an iPhone device for instance. The reason is the bytes are obviously reversed as the string ‘Good’ has the letter ‘d’ as the lowest byte and that ends up being at the first byte of the string so we end up with “dooG”. That’s not good though, is it? So we just have to swap the bytes around to get the proper string:

Screen Shot 2013-03-24 at 19.31.08

Easy peasy, aye? The highlighted line is very important. That line swaps the order of bytes in the integer. Also note that I am using the calloc function as it will not only allocate the memory but also set the byte values to 0x00 in the memory for us so we don’t have to do it manually or with another procedure.

DocBook Video Tutorial 2 – DocBook to PDF with Xcode and XMLLINT

In this tutorial you will learn:

  1. Downloading DocBook Documentation
  2. Writing DocBook in Xcode
  3. Validating DocBook XMLs
  4. Generating PDF from DocBook XMLs

To download MacPorts, go to: http://macports.org/

To install XMLLINT categories, use this Terminal command:

sudo port install docbook-xml xmlcatmgr

To validate a DocBook XML, place these commands (as demonstrated in the video) into an executable file and run by passing the name of the file to validate as an argument:

fileToValidate=”$1″

export SGML_CATALOG_FILES=/opt/local/etc/xml/catalog

xmllint –valid –noout –catalogs $fileToValidate

Questions? let me know and watch for the next videos about DocBook