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.

Deprecating Properties, Methods and Enumerations in Objective-C – LLVM/Xcode Tips and Tricks

A lot of times when working in a team, you would write a class, or add an enumeration to an existing class or add a new property. Some times, you might need to tell other programmers who work on the same code-base that a property, enumeration or a method that you wrote is now becoming deprecated and perhaps a new version of the API introduces a new property/enumeration/method that programmers need to use instead.

This is where LLVM can be very useful. Obviously, if you are the only programmer in a project and not developing an API for others, this might not be of much use to you. But if you are used to working in a team, knowing this trick is an absolute necessity.

Let’s say we are writing an enumeration that at the time of writing (let’s assume at the time of writing this enumeration, iOS 5 was the latest iOS version), everything was fine like so:

Our enumeration works on every iOS version for now

Now let’s say iOS 6.0 is introduced and you are thinking to yourself “Oh that Ultra Fast item in the enumeration is just not very good and I think I have to remove it”. So what to do now? The solution is using __attribute__ like so:

Deprecating an enumeration value in Objective-C

Deprecating an enumeration value in Objective-C

The availability attribute can have the following keys:

introduced: this will be the version of the operating system (iOS, Mac OS X) where the API was introduced first.

deprecated: this is the version of the operating system where the API was deprecated (ready to be removed from the API in later versions of the API)

obsoleted: the version of the operating system where the API is completely removed and is no longer supported.

message: the message to display to the programmer in Xcode.

As you can see, right in front of the availability attribute, we can write “ios” or “macosx” to denote if an API is (un)available for iOS or Mac OS X respectively.

Now let’s assume iOS 7.0 comes along and we want to completely stop our support for the “ultra fast” enumeration value. All we have to do is to add the “obsoleted” key to the availability attribute like so:

Completely removing (obsoleting) an enumeration item from iOS 7.0I hope you enjoyed this tutorial. Let me know if you have any questions 🙂

Jenkins doesn’t load under OS X Mountain Lion (or Server)? Here is the fix!

Answer: your machine doesn’t have Java installed on it. This happened to me. Jenkins installed fine, but then when I opened it on its default port of 8080, nothing happened.

To fix it, open Terminal and type “java” without the quotation marks.

This will tell you that you have to have Java SE installed on your computer and then will download and install Java SE automatically for you. Done! 🙂

The REAL location of the CIFilter in OS X Mountain Lion (Development)

So you are trying to use CIFilter for the first time and the documentation tells you that this class sits in the following location?

Library/Frameworks/CoreImage.framework

Image

The documentation is LYING its ass off to you. The real location of this class is in the QuartzCore framework. Import QuartzCore.framework and you are sorted.

Image

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…

Find cpp, c++, clang++ and clang in Xcode 4.3.x and Higher

 

So you have some scripts that use “cpp”, “c++” or “clang” and etc but you cannot find them in your path? Not to worry, read on…

With OS X Lion and sandboxing, you can find your c++ compiler using the “find” command:

find /Applications/Xcode.app/ -name “c++”

This will return something like:

find /Applications/Xcode.app/ -name “c++”
/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++

The one in the “bin” folder is the one we are looking for so the folder that contains our toolchain is here:

/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/


Navigate to that folder to do some inspection:

cd /Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/
ls -la c++
lrwxr-xr-x 1 root wheel 7 19 Jun 22:41 c++ -> clang++
ls -la clang++
lrwxr-xr-x 1 root wheel 5 19 Jun 22:41 clang++ -> clang
ls -la clang
-rwxr-xr-x 1 root wheel 22985632 19 Jun 22:41 clang

You can see right away that c++ is a symlink to clang++ and clang++ is a symlink to clang. What a mess!

Now let’s find “cpp”:

ls -la cpp
ls: cpp: No such file or directory

Oopsy daisy, it’s not there. That’s one of the issue a lot of developers will be facing. So let’s remedy this by first creating cpp as an alias. We want this alias to be permanent between Terminal sessions so we put it in our bash_profile and we also have to make sure the toolchain bin url highlighted above is in our path:

vim ~/.bash_profile

And then change the content of your bash profile so that your path includes:

/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/

and also create an alias for cpp to point to clang. The contents of my ~/.bash_profile now looks like this:

export PATH=~/bin/:/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/:$PATH
alias cpp=’clang’

Once you are done, press Escape in vim and then type :wq and press enter. That will “write” and then “quit”.

After you are done, close Terminal and open it again and type cpp:

cpp
clang: error: no input files

Lovely, it’s working. Any questions? Leave them down below 🙂

Installing Git on Mac OS X

If you want to install Git on your OS X Lion, whether you have Xcode or not, you can head to:

http://git-scm.com/download

There choose your platform and the install process will start automatically. This will download a .dmg package to your machine. Simply follow the installation process. After installation, you should be able to find Git in the following folder:

/usr/bin/git

And since this folder is already added to your path, you can simply access it in Terminal by typing git