Posts Tagged ‘Objective-C’

NSLog Format Specifiers

February 19th, 2012
%@     Object
%d, %i signed int
%u     unsigned int
%f     float/double

%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

%lld   long long
%llu   unsigned long long
%Lf    long double

Static Libraries & Workspaces

November 29th, 2011

They say that you should not duplicate your code, but when it comes to iOS development what I see is a lot of duplicated files from third party libraries… For example, JSON and ASIHTTPRequest. So we end up with loads of duplicated files or, if not duplicated, very similar, differing in version.

Static Libraries would help the programmer with this problem.

To create your static library project, you choose the Cocoa Touch Static Library template under Framework & Library. What you need to note about this project is:

  • Set the headers (you want to be public) to public. This can be set during the Copy Headers under Build Phases of your static library target.
  • Set the Installation Directory to $(BUILT_PRODUCTS_DIR).
  • Skip Install should be set to Yes.
  • Public Headers Folder Path should be set to $(TARGET_NAME).

This project can then be used by other projects. I recommend you to set up a workspace for this. You can either save a current project as a workspace or create a workspace and drag and drop your project (the .xcodeproj file) to it. Be careful when you drag the static library project not to drag it inside another project. It should be at the same level of the main project.

Then, follow these steps for your project:

  • Add your library to Link Binary With Libraries.
  • Set User Header Search Paths to $(BUILT_PRODUCTS_DIR) recursively.
  • You may need to add -ObjC to Other Linker Flags if you use categories. You may also need to add -all_load and -force_load in some situations.

Good bye, EXC_BAD_ACCESS!

August 28th, 2011

The EXC_BAD_ACCESS error means that a message was sent to a point in the memory where there’s no instance of a class to execute it. Thus “bad access”. Most of the times, the message was sent to an abject that has been already released or was never initialised.

In the over released objects situations, zombies are very handy… When you enable this feature, a dummy object (a zombie) is kept on the place of every released object, allowing to debug objects which were released already. To enable the zombies, edit the scheme: in “Run”, open “Arguments” tab and add NSZombieEnabled with value YES in the “Environment Variables” section. Voila!

Important: remind not to leave the zombies enabled when you submit your app to the App store. Also, it’s a good practice to disable them if you don’t really need them.

Correct use of MapKit

June 24th, 2011

I’ve seen some apps being rejected because MapKit was used in a way it violates the Google’s terms of service for Maps. Commonly it is because the Google logo is hidden or covered.

When using MapKit you need to make sure that Google logo will be visible always. You can prevent it being hidden by resizing operations using the following resizing mask:

mapView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);