Swift Weekly – Issue 06 – The Swift Runtime (Part 4) – Generics

Generics are pretty cool. They let us do complicated stuff that many programmers don’t want to deal with sometimes and want to stick with traditional means of achieving the same goals but using basic ideas in OOP. In this edition of Swift Weekly, I won’t teach you about generics eventhough you may just see the examples and learn generics anyways. What I will teach you however is how generics are compiled at the assembly level buy the Swift compiler.

I am going to use the release version of the code to make sure the output assembly is as optimized as possible so that the optimization level is set to -O in the output when your Swift files are being compiled. Also my swift -versionshows this:

Swift version 1.1 (swift-600.0.54.20)
Target: x86_64-apple-darwin14.0.0

I am using the latest beta of Xcode, aka Version 6.2 (6C86e). Let’s get started.

Note: I am going to get rid of some of the assembly code that is not relevant to the main point of this week’s objective.

Continue reading this article on GitHub.

Build and Run Python Scripts Using Xcode 5

Edit 1 (20th October 2014): Xcode 6.1 instructions can be found here: https://vandadnp.wordpress.com/2014/10/20/building-and-running-python-scripts-with-xcode-6-1/

In this video, I’ll demonstrate how you can use Xcode 5 to build and run Python scripts.

Building and Running Python Scripts with Xcode 4.x

 Edit 1 (20th October 2014): Xcode 6.1 instructions can be found here: https://vandadnp.wordpress.com/2014/10/20/building-and-running-python-scripts-with-xcode-6-1/

So you have installed Python and you want to use Xcode to write and run your Python scripts… is that correct? Ok then, follow these steps to create a simple project and run it with Python using Xcode 4.x…

  • Open Xcode and select File->New->Project…
  • From the left hand side, under the OS X category, select Other
  • Now on the right hand side, select External Build System and press the Next button, as shown below

Click to Enlarge

  • Now open a Terminal window and type which python in it, and keep the results in mind or just keep Terminal open for future reference, as shown below. This will tell you where Python is installed on your system.

Click to Enlarge

  • As you can see, in my case Python is installed under /usr/bin/Python
  • Now back to Xcode, set the following values on the second screen of project creation:
    • Product Name: test
    • Organization Name: company
    • Company Identifier: com.company
    • Build Tool: enter the path to Python which we retrieved from Terminal. I have entered /usr/bin/Python (see below)
  • Once you are done entering all four values, press the Next button.

Click to Enlarge

  • Once you pressed Next, now you should choose where you want to save your project, save it somewhere that makes sense for you. I am going to save it in ~/Desktop/Development/Projects/Python/Sandbox/test/ as that’s how I have structured my development environment. Once you are happy with the folder that you are saving your project in, press the Create button in Xcode’s dialog, as shown below

Click to Enlarge

  • Now select the “test” target from the breadcrumb menu on top left corner of Xcode’s main window and choose Edit Scheme, as shown below

Click to Enlarge

  • In the Edit Scheme screen, on the left hand side, choose the Run option. Then on the right hand side, choose the Executable section and press the Other… item. This will open an Open dialog for you asking you to choose the binary of Python. For me, Python was sitting in /user/bin/python as you saw earlier. So in Mac, I press Cmd+Shift+G to “Go to Folder” and then I type in /usr/bin/, as shown below and then press Go:

Click to Enlarge

  • Now that you are in the folder that contains the Python binary, find and pick the binary and press the Choose button
  • Back in Xcode, on the same Scheme window that we were on before, under the Debugger section under Info, make sure that you choose None. This is very important.

Click to Enlarge

  • Back in Xcode, on the same Scheme window that we were on before, navigate on the right hand side to the Arguments tab and under the Arguments Passed On Launch, press the + button and type test.py, as shown below:

Click to Enlarge

  • Now go to the Options tab, under the Working Directory, click the Use custom working directory and then put the full path to where you are intending to save your Python files in this project. My project is at ~/Desktop/Development/Projects/Python/Sandbox/test/ and I’m intending to save my first file as ~/Desktop/Development/Projects/Python/Sandbox/test/test.py so I will choose ~/Desktop/Development/Projects/Python/Sandbox/test/ as my working directory, as shown below:

Click to Enlarge

  • We are now done with the Schemes. Press the OK button to close this window.\
  • In Xcode, from the Menus, choose File->New->File…
  • From the left hand side of the dialog, under the OS X section, choose Other and then on the right hand side, choose Empty and press the Next button, as shown below:

Click to Enlarge

  • In the next screen, make sure that you set the value of Save As as test.py. This is what we have been preparing our Scheme for, that we are going to have a file called test.py that needs to be passed to the Python interpreter. Have a look at the following screenshot. Once you are done, press the Create button:

Click to Enlarge

  • Now enter the following code in your test.py file: print “It works!”, as shown below:

Click to Enlarge

  • As the final step to make sure everything is working fine, in Xcode, from the menus, choose Product->Run, the Debugging Area will popup and show you the output of the print command like so:

Click to Enlarge

Lovely, it’s all working. I hope this tutorial helped you. If you have any questions or suggestions, please leave them in the comment sections down below…

scp and cronjob (without password) on OS X

I wanted to copy some files from my computer over to a server every minute, as a cronjob. I searched around the internet but the solutions were scattered all over the place and a lot of “do this and do that” so I decided to put things together in this comprehensive video.

In this video, you will learn:

  1. How to set up your RSA private/public keys.
  2. How to set up passwordless SSH access to your server.
  3. How to “scp” files from your client to your server under your user name.
  4. How to set up a cronjob to copy your files over to the server once every minute.
If you have any questions, leave them down here and I will do my best helping

Git from command-line after installing Xcode on OS X Lion

Xcode 4.3.x or newer comes with Git but the problem is when you install Xcode on your machine (OS X Lion or newer), Git’s path won’t be added to the user path which means if you run git from your command-line, your system will say:

-bash: git: command not found

Xcode’s installation of Git is at the following location on your machine:

/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/

With the git binary sitting here:

/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git

To add this binary to your path (which will allow you to run “git” from any directory on your system), go to terminal and type this command:

export PATH=”/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/”:$PATH

There is one caveat to this approach and that is the PATH will only be changed in your current running instance of terminal. As soon as you close terminal and open it again, you will have to enter the above command again to get access to the git app. So what is the proper solution? You will have to add the above “export” command to the .profile file in your home directory. The .profile file gets read every time you open terminal. So open a terminal instance and type the following command:

cd ~/

And then type this command:

ls -la | grep “.profile”

We are trying to find out if we already have a file named .profile in our home directory. If after running the above command you won’t see anything getting printed to the terminal, use the following command to create a new .profile file. If you already have a .profile file, skip this command:

touch .profile

Now open the .profile with this command:

open .profile

Now add the git path to the PATH variable in the .profile file so that your .profile content will look something like this (it really depends on what you already have in this file. I am assuming your .profile file didn’t exist until now and you just created it):

export PATH=”/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/”:$PATH

Save your changes to the .profile file and close terminal and open it again. Now whichever directory you are in, in terminal, you can use the git command. Good luck.

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