Archive for the ‘Obj-c’ Category

AppCode File Template for CoreGraphics and PaintCode

Wednesday, November 21st, 2012

In my last post I setup a CALayer subclass for CoreGraphics and PaintCode.

I created an AppCode template that I am sharing here.

The header file template:

Implamentation file template:


Vector Graphics in iOS with CoreGraphics and PaintCode from an SVG file

Monday, November 19th, 2012

Since I started iOS development there has always been one thing missing for me. That has been a vector graphic file format. There is no format currently natively supported by apple.

I did some digging and found SVGKit. This is a very nice library that loads in .svg files. It even supports SVG animations. Not to mention its free. So I had to share this. However it can be tricky to set up.

In the end I settled on using PaintCode. PaintCode is a vector based graphics application that generates CoreGraphics Objective-C. PaintCode will also load in SVG files.

The idea is you load an SVG file into PaintCode. Edit it if needed then copy the Objective-C code into your project.

There are different ways you can include it in your projects. I would like to use it in layer of the CALayer flavour.

What I have done is create a CALayer subclass. That all you have to do is past in the PaintCode code.

For convenience I create all my vector assets at 100×100 pixels. I then use a scale to get it to the right size.

This assumes you have PaintCode and have already got the code ready. Also assumes the image is 100×100.

Step 1:
Add the “QuartzCore.framework” to your project.

Step 2:
Create a new file using the “Objective-C Class” template for iOS. USE CALayer Subclass

Step 3:
Add the bellow line to to your Header file(.h).


Step 4:

Add the bellow code to the implementation file(.m). Note: see line 17 if your image is not 100×100 pixels

Step 5:

Add the CoreGraphics code to the drawImage method.

Step 6:

Add this layer to a view that is currently on the view stack.


Thats all you need.



I created an AppCode template for this.


Miller Cylindrical Projection with source for (Long, Lat) to (x, y)

Wednesday, May 2nd, 2012

Recently I have been trying to get a custom map working with Longitude and Latitude that plotted in the x and y in iOS. It also needed to work with a zoom and offset. It was a bit of an argus journey but I got there in the end.

What you will need is a projections type(map type) and the formula for the conversion. Ideally a projection type that compensates for the curvature of the earth.

I settled for a Miller Cylindrical projection. The problem with the maps is finding an accurate map. You can use maps with offsets but getting started it helps if you have an accurate map. I ended up going through a lot of maps before I found an accurate one. It had a white border that I had to crop off but otherwise it was perfect.


I scaled it up for testing purposes up to a height of 6563 and a width 8933 cropped.

Here is the Objective-C code and little C to get he job done:

As you can see the the hight and width is hard coded. What if your map needs an offset and you also need to have a dynamic size to compensate for zoom levels.

NO MORE LOGS!!!!! (Using breakpoints in xCode)

Thursday, October 20th, 2011

I cant count the number of times I have compiled an app and found my self lost in logs. This is not not just an xCode issue I have come across this in every language/IDE I have used. For some reason we like to log it and leave it.

We give our selves all maner of excuses for not removing log statements. Whether its needed so every developer on the project knows whats happening or simply not  having time to go back and remove them. We do end up accumulating a lot of logs over the span of a project.

Dont get me wrong a well placed log can make debugging a problem much easier. There are other ways to do it though. Using breakpoints in xCode rocks logging as well as giving you a few extra perks.

Lets start with right click and editing a breakpoint to see:

Now this is one of my favorite tools in xCode. Off that bat it allows for conditional breaks. The above break for example will not break unless the variable newPos is equal to 42. This can help with keeping an eye on a value to make sure it does not act in appropriately and if does you will get the break. How ever clicking the actions text above marked with a blue arrow will unleash the true power behind the xCode breakpoint.

Out of the 5 above options I only use two with any frequency.  The “Debugger Command” which will allow you to log, inject calls and set variables.  The “Log Message” more for english messages and keeping track of the frequency of code used.  OH ya one more thing “Log Message” supports text to speech.

Lets start with the “Debugger Command”:

“po someObject” looks like it would print the description of someObject. Thats not all though. Dont forget the conditional above it “newPos == 42”. Combine the two and you will only print someObject to the consol when newPos is equal to 42. If you uncheck the bottom checkbox “Option” it will log the description with out breaking.

You can also use the field to more than log. Be careful  though as this can easily cause problems if you forget to remove or disable the break.

The command could be to call a function or set a variable like this: “newPos = 13”

If you were to forget to remove a command like that it could burn a lot of time tracking it down.

Moving onto the “Log Message”: 

This obviously prints a message. If you look under text field to the right you can see a key. Above I used the %H which will be replaced by the number of times the break has been hit. Also if you remember to uncheck the Option at the botton it wont break but will simply log at the break.

You also get the option to have the message read out loud. This is useful if you have to keep your attention on the app and cant keep an eye on the logs.

This covers the two basic implementations. What if you need to do more than one thing like run multiple commands?

Stacking Actions:

Pressing the + button as indicated above by the orange arrow. That would be the top arrow as the bottom one is yellow. (my color selection is rubbish) You get another action that runs at the same time.  You can run any number of commands in the one break which can provide very rich loggin and debugging capabilities.

Managing your breaks:

Toggling the button indicated by the green arrow you can enable or disable all your breaks in one go.

The only way to manage large numbers of logs is to search nslog and edit the file. This will cause the file to have been touched wich will obviously need versioning. Fortunately if you open the break window indicated by the blue arrow above. A list of all your breaks and their locations can be found.

Each break can be enabled or disabled by pressing the break symbol to its right as indicated by the purple arrow above.

Sharing Breaks:

Most projects are version controlled with either SVN or GIT. In all of my projects we make sure to ignore the personal settings folder in the .xcodeproj file. Which incidentally is just a folder. You can right click “Show its Contents”. In there you will find a folder called “xcuserdata” this will contain the personal profile for each developer. It will contain  the developer specific xCode settings like layout. This folder is ignored and never committed or pushed.

However if you were to share a breakpoint.

Right click a break point and select share as seen above. It creates a new folder in the project file. The “xcshareddata” can then be used to share break points with other developers . Say you had a particularly nasty bug and set up some particularly elaborate breaks they would be easily passed on to another developer who could help you or continue on the work.

The description method

Tuesday, October 11th, 2011

Any object can implement the description method that is used to return a description of the contents of a receiver. The problem i have always found with this method. Is keeping the method up to date during the development process. Also getting other developers working on the project also keeping it up to date.

With that problem in mind I put together this method that will automatically generate the string and list all available properties in the the receiver. It will print out when the instance is used in NSLog or or printed using the GDB debugger commands in my previous post.

You can also find it on my custom xCode 4 templates here on github. Right at the bottom the class.

xCode GDB Debugger Commands

Thursday, September 22nd, 2011

One of the things that I struggled with going from eclipse and Visual Studio to xCode was the the debugger.  Most of the objects have memory references rather than values. For a long time while developing iOS apps all i would get, or though I had was one cryptic error message. Till I discovered po which stands for print object. This allowed me to print out the contents of an object which can be very useful.

Here is  list of gdb commands: (Warning properties are not identified by the debugger so dont use anything with dot notation)

po – print object – can be used to print out the likes of NSStrings and NSDictionaries
use: po someObject

print – prints base types – used to print out to the screen ints floats and the like
use: print myFloat

set – set a variable – allows you to set variables
use:  set someString=@”test”

call – calls a method – can be used to call methods or the properties getters and setters
use: – call [someObjectInstance withString:@”newString”];

n – Next – always you to travers your code during breck
use: n

There are 2 main places you can use the commands.

The first is the consol output during a break or crash just after the gdb:

The second is in a break point first set a break point then click the “Click to add action text” ass seen bellow.

Now you can add you command in the newly available text field that will always be triggered at that point in the code.

Carful with the break point triggers not to confuse your self if you put sets or calls in there.

Setting up xCode 4 Projects

Friday, June 24th, 2011

Every time I start a new xCode project i do it a little differently. I include files in some and settings in others. I have decided to put them all here to make sure i don’t miss any. If you have any other project settings, scripts, snippets or useful utils please drop a comment.

1. Enabling and dealing with Zombies:

This allows you to find errors that come from delloced objects being called upon. That normally just gives you the useless error bad access. with this enabled it should take to a class or function when you see the bad access error. This can be dealt with in 2 ways either enabling zombies in the editor thus allowing you to debug it in xCode or using instruments. If the first method does not work for you try the second.

Method 1 xCode Debug:

  1. get to the run settings by press: cmd+alt+r
  2. from the left nave select “RUN”
  3. select “Arguments” from the right tab system
  4. expand if not already expanded the environment variables
  5. add NSZombieEnabled the name parameter and YES to the value parameter

Method 2 instruments Debug:

Rather than take you through it Mark Johnson has created a good video that talks you through it.

Check out his blog post for more details:

2. Replacing NSLog with PSLog

PSLog is a NSLog replacment. I have seen it arround for a while now. Basicly it gives you a bit more info like where log is called from. The only problem with it, is the dependancy it creates on having to keep importing PSLog in every one of your projects.

I modified it so you can use NSLog in your code and at compile time in debug mode it replaces your NSLogs with PSLogs. During deploy it replaces NSLog with comments. This way it removes any depandacies on the PSLog function.

How ever this only applies to the PSLog function it self none of its expanded counter parts like PSLogDebug. Using PSLogDebug will maintain a dependancy.

Check it out:

If you #import “PSLog.h” in the .pch file in the support group in your project you wont have import it in every file you want to use it. which Helps the whole dependancy issue.

TODO, FIXME converted to warnings

I duno if you noticed but if you add a //TODO: or //FIXME: comments it appears  in the function menu. The problem with that though is you need to be in the class to see the list. Ideally what I wanted was a list of todos that I can see them all in one place. You can do that by adding a bash script that adds them as warnings.

  1. Select your project in the left hand menu
  2. Select your project in the target right hand menu
  3. Click “Add Build Phase” in the bottom right.
  4. Select “Add Run Script”
  5. Past in the script below and your done 🙂

i think this guy might have come up with it:

Snippets are one of the things that stood out to me in xCode. Here are a few of mine:

Completion shortcut – log
Completion scope – function and method

Completion shortcut – imp
Completion scope – processor directive

If you want to make a variable just stick them in <#here#>.

Crash Log Server

Sends the crash report to a server with a single line of code. I have not tried the push notifacsion but every thing else seems to work resonably.

Please let me know if you find anything else that helps keep a project in shape.

cocos2d: WARNING: format is not supported for CCSpriteFrameCache

Monday, September 13th, 2010

I have been working away at same iPhone game development.

I found a nice sprite sheet editor zwoptex: which is compatible with the engine I was using cocos2d:

Little did I know there was some version issues between the exporter and the engine. Zwoptex exports a png and a plist that contains all the meta-data for the sprite sheet. If you are getting the bellow mentioned error you might be reading online various suggestions like installing older versions of zwoptex or a new versions of the  engine.


As it turns out all you have to do is un-check  rotate from inside zwoptex publish the files. Edit the plist generated by zowptex and change the root>>metadata>>format number from 2 to 1 save clean your build recompile and your done.

cocos2d error:

ocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:

Full Error:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:’
2010-09-13 16:01:36.353 TileMap[12915:40b] Stack: (

Code Based Localisation

Friday, August 14th, 2009

Code Based Localisation in xCode 3.0+ SDK

Code based localisation focuses on any strings you may have in your code. Finding these strings and making them dynamic. This is a contrast to how localisation is done in say Flash where the localisation methods are normally set up by the developer. Kind of like Flex.

If you are after NIB Localisation checkout this post here

A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the language like “English”, “French” or “Japanese”. The second is the local project abbreviation “.lproj” putting them together you get folders by the name “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.

Basically any text that is set via code needs to be set using NSLocalizedString this takes 2 strings. The first is the the copy that needs localisation. The second string it takes is the comment that appears above the name value pair.  see example:

Once you have written your code using NSLocalizedString with all the necessary default copy you will run a terminal script that will pull out the default strings strings to create a .strings file which you would then localise. “genstrings” is the command line tool used to interrogate your .m files to find what needs to be localised.

Once you have your localised .strings file you simply copy it to the different local folders updated the copy to the respective local and bobs  your uncle, its done.


  1. create a new project in xcode it should not matter what type but I will use an iPhone View-Based Application.
  2. add the bellow code to the viewControler of the newly created project to quickly add some uilabels
  3. Open the terminal window and navigate too the root of you project folder. This is the folder that contains your Classes folder.
  4. Run “genstrings ./Classes/*.m” in the terminal which will scan every .m file in your Classes folder for any references to NSLocalizedString and added them to a Localizable.strings file which should appear in the root of your project folder.
  5. In the finder navigate to the root of the project folder and then drag and drop Localizable.strings from the Finder to the resource folder in XCode remembering to insure that the “Copy items into…” check box is unchecked. Also make sure that Text Encoding is set to UTF-16 to insure all languages are covered.
  6. Finally press add.
  7. Right-Click the newly added Localizable.strings then select “Get Info”.
  8. Select the General tab from the “Get Info” panel
  9. Click “Make file Localizable”
  10. Click back on the general tab
  11. Click “Add Localization”
  12. Enter your new Locals “French”(from the English name section of the link) and or see here for full list:
  13. Once you have added the Locals for Localizable.strings close the “Get Info” window
  14. Run your app go to the settings panel>>Genral>>International change your local and it should display your localised text.

NIB Localisation

Friday, August 7th, 2009

NIB Localisation xCode 3.0+ SDK

There are 2 ways to localizes iPhone application and regular OSX applications. The first is localizing the NIB file. This automatically hard codes the localized copy into a NIB file. This works as of iPhone SDK 3.0 should work on previouse versions and future versions but that was my version at the time.

You can also localise with code but that is not the focus of this point check back for Code based localisation maybe next week.

A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the Language, like “English”, “French” or “Japanese”. The second is the local project abbreviation lproj. Putting them together you get “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.

NIB localisation works by taking a NIB that was built for one local with all the copy hard-coded into the NIB then scanning the NIB with the command line tool ibtool. Extracting the copy to a strings file. The strings file is then copied and localised for each local. Then another script hard codes the localised strings into the respective NIBs. This process only uses the generated strings files once to get the NIBs localised. Once this is done the Strings files are no longer necessary.


  1. create a new project in xCode it should not matter what type but I will use an iPhone View-Based Application.
  2. open up the view controller NIB in interface builder add some labels and buttons add some copy to them
  3. save and close interface builder
  4. in xCode right click the view controller NIB and click “Get Info”
  5. make sure you are in the genral tab then click “Make file Localizable”
  6. select the “General” tab again as may change after clicking “Make file Localizable”
  7. click “Add Localization”
  8. Enter your new Locals like “French” from the English name section of the link see here for full list:
  9. close “Get Info” Panel
  10. open up terminal
  11. navigate to your xCode project folder in terminal
    1. now we are going to run the command line script above to scan your nib file and generate a strings file below I have broken down the command so you can replace what u need
    2. I will use    ibtool –generate-strings-file NIBCopy.strings English.lpoj/LocalizationNIBViewController.xib
    3. ibtool is the command line tool called Interface builder tool that is installed by default when u install xCode
    4. –generate-strings-file   is the ibtool command that creates a strings file from a nib
    5. NIBCopy.strings is the name I  have chosen for my strings file u can  rename it if you want but maintain the .strings at the end
    6. English.lpoj/LocalizationNIBViewController.xib    is the path from the root of my xCode project folder to the NIB file i want to get scanned you must rename this to match the path and name of your NIB
    7. in your case you should only have to replace the NIB path from sub step 6
    8. so you should end up with something similar to ibtool –generate-strings-file NIBCopy.strings English.lpoj/LocalizationNIBViewController.xib
    9. it is likely that you will get the error “Unable to find either a loadable database or a Nodes.xml configuration file” i get it too don’t worry about it assuming your strings file was generated it will contain the name value pairs from the nib
    10. check to make sure your NIBCopy.strings is created if so move on else go back to sub step one and try again maybe type out the whole request manually in-case copy pasting messed it up a bit
  12. now copy the NIBCopy.strings to the French folder and any other locals folder you have created
  13. update and localise the NIBCopy.strings in the respective folders
  14. once again load up the terminal window
  15. navigate to the root of your project folder in the terminal
    1. now we are going to run the command line script above to take your NIBCopy.strings that contains your localised copy and hard code it into your localised NIBs
    2. ibtool is the command line tool called Interface builder tool that is installed by default when you install xCode
    3. –strings-file the ibtool command that refers to a strings file
    4. French.lproj/NIBCopy.strings my french localised strings file
    5. –write the command that writes a NIB file based on a Strings file and a NIB file
    6. French.lproj/LocalizationNIBViewController.xib the NIB file that will be writen/updated with the localised copy
    7. English.lproj/LocalizationNIBViewController.xibthe NIB file that will be the template for the localised french NIB file above
    8. it is likely that you will get the error “Unable to find either a loadable database or a Nodes.xml configuration file” i get it too don’t worry about it in fact that is how i know it worked 😉
    9. repeat sub step 1 to 7 for each localised NIB file local
  16. to test this either open up the localised NIB in interface builder or run the app and change the language settings in the simulator or on your developer iPhone
  17. the strings files are no longer needed as the copy is now hard-coded into the NIB