Swift Weekly – Issue 07 – The Swift Runtime (Part 5) – Operators

I thought I’d write about operators a bit in this issue. I don’t like to teach how operators work, but rather show you some cool things that we can do with operators. but then again, many websites do that already. you can just search online and find hundreds, if not thousands of blogs/websites that can teach you how to use operators and how to create your own in Swift. so how can i be different and offer something else? well, we will talk about operators in this issue and how to write your own, but, i will also show you how custom operators are compiled by the Swift compiler.

Continue reading this article on GitHub by clicking here.

Swift Weekly – Issue 04 – The Swift Runtime (Part 3) – Operators

I have always been interested in finding out how different compilers work with basic operators such as +, -, % and so on. This week on the train I was thinking that it would be nice if somebody could explore how Swift deals with operators so, long story short, I decided to do it myself.

In this edition of Swift Weekly, I will show you how the Swift compiler works deals with (system and your own) operators and how to use operators to ensure you get the maximum performance.

Note: in this edition of Swift Weekly, I’m going to change things a little bit and instead of building for the debug configuration, I am going to build for Release to ensure that the assembly code that we are going to analyze is as optimized as what you will get when you release the app for the App Store. Optimization is hence enabled and the assembly output is long. That means setting the Optimization Level in your build settings to Fastest, Smallest [-Os] to ensure you get the export GCC_OPTIMIZATION_LEVEL=s export when you build your project.

Note: to ensure that the assembly code which we will look at is clean and nice without too much unnecessary code, I will remove bits and pieces of it but will keep all the assembly code that is relevant.

Continue reading this article on Swift Weekly’s home page here.

Swift Weekly – Issue 03 – The Swift Runtime (Part 2) – Enumerations

This is the second article in the Swift Runtime series of the Swift Weekly. In this article, we will dig deeper into the Swift Runtime and how the compiler deals with producing code for enumerations. Saturday morning writings are always fun! Let’s get this show started.

If you are an Objective-C or Swift programmer and have not done any Assembly programming or are simply not concerned with the low-level details of this article, jump right into the Conclusion section at the end to get the juice of this article.

Continue reading this Swift Weekly issue on Github.

Swift Weekly – Issue 02 – The Swift Runtime (Part 1)

In this edition, I wanted to write about arrays and dictionaires and take the easy route. But I thought to myself: wouldn’t be cool if _somebody_ dug deep into the Swift runtime for crying out loud? Then I thought that I cannot wait for somebody to do that so I’m going to have to do that myself. So here, this edition of Swift Weekly is about the Swift runtime. At least the basics.

Please note that I am using a disassembler + dSYM file. I am disassembling the contents of the AppDelegate with some basic code in it and then hooking my disassembler up with the dSYM file to see more details.

Also in this article I am testing the output disassembly of Xcode 6.1 on the x86_64 architecture, not ARM which is available on iOS devices.

Continue reading this article on Swift Weekly’s Github page: https://github.com/vandadnp/swift-weekly/tree/master/issue02

Swift Weekly – Issue 01 – Pointers

I have started working on a new project called Swift Weekly. The reasons behind this decision are plenty. I’ve noticed throughout years of publishing books that the best way to learn is to teach. In my quest to learn Swift better and better every day I have decided that I want to write about it. My son has recently been born so I am very busy at home too which means that I don’t have much time to write. So the weekly nature of Swift Weekly is perfect for me. Also, I believe in giving to the community so that is the third reason.

Swift Weekly issue 01 focuses on the niche subject of pointers in Swift. Swift Weekly is all hosted on GitHub and you can find it here:

Swift Weekly on GitHUb

Have a read through the first issue and see what you think. Ideas and suggestions are welcome. Also spread the word and share this with your Swift lover friends!

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