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!

Building and Running Python Scripts with Xcode 6.1

Follow these steps:

  1. Open Xcode
  2. Create a new project and select Other from under the OS X category when the dialog appears, and then choose External Build System:

    Screen Shot 2014-10-20 at 20.20.47

    Tap to enlarge

  3. In the next page, give your project a name “product name” and then in the “build tool”, choose the path of your Python interpreter. If you don’t know where your Python interpreter is, open Terminal and type in which python to get the path to the interpreter, like so:

    Tap to enlarge

    Tap to enlarge

  4. Then save your project on disk
  5. From the Product menu, choose Scheme and then Edit Scheme or just Option-click the little Play button on top left of Xcode. Now you should see the Edit Scheme screen which looks like this:

    Tap to enlarge

    Tap to enlarge

  6. Now tap on the Info tab on top of the dialog and then press on the Executable combo-box (which currently says “None”) and then from the list, choose “other…

    Tap to enlarge

    Tap to enlarge

  7. An open-dialog will appear waiting for you to select your build tool, again! This is a bug in Xcode. So press the Cmd+Shift+G button in the open-dialog and when the “Go to the folder” dialog appears, enter the path of your Python interpreter again like so:

    Tap to enlarge

    Tap to enlarge

  8. Once you are done, press the Go button and then press the Choose button
  9. Back in the Edit Scheme dialog, uncheck the “Debug executable option as you don’t want Xcode to attach the LLDB debugger to Python. That’s not useful. This step is very important.

    Tap to enlarge

    Tap to enlarge

  10. Now tap on the Arguments tab and then under the “Arguments Passed on Launch”, press the + (plus) button and then type in “test.py” without the quotation marks, like so:

    Tap to enlarge

    Tap to enlarge

  11. Now tap on the Options tab and then under the “Working directory” section, tap the “Use custom working directory” and then tap on the little Folder button. Once the open-dialog appears, choose the root folder of your Xcode project:

    Tap to enlarge

    Tap to enlarge

  12. Now press the Close button to close the Edit Scheme dialog
  13. Press the Cmd+N combination on keyboard or just select from the menus, File->New->File…
  14. In the New file dialog, from the left hand side, choose OS X and then Other and then choose Empty and then press the Next button:

    Tap to enlarge

    Tap to enlarge

  15. Name your file “test.py” (without the quotation marks) and then ensure that you are saving it under your project’s main folder, the same folder that you set your “Working directory” to a few steps ago. Once you are done, press the Create button.

    Tap to enlarge

    Tap to enlarge

  16. Write a simple Python script in your “test.py” file like so:

    Tap to enlarge

    Tap to enlarge

  17. Now run your application and have a look at the console in Xcode to see your Python script successfully executed:

    Tap to enlarge

    Tap to enlarge

That was it really. Good luck everyone. If you have any questions, just let me know.

iOS 8 Swift Programming Cookbook

Hi lovely readers :-)

My iOS 8 book is out and it is ALLLLL new, rewritten to use Swift with tons of new stuff in it.

Check it out, it is 50% off. Click here to go to O’Reilly’s website to purchase the book.

Also a surprise, I have also done a video course to teach you all about iOS 8 programming with Swift. THAT too is 50% off for a limited time. You can click here to get the videos.

Happy coding everyone