Tag Archives: objective c

Using code comments in Xcode 5.0

Code comments are great! We all know this. However, they could be even better with Xcode 5.0. This post will give brief information on how to write proper code comments, so they will appear in autocomplete popups and Quick Help inspector.

Actually, this comment style is pretty typical. There are several keywords, which should be used. So, let’s check those.
Continue reading

Sending message to nil

Recently, I’ve came across Greg Parker’s post on Sending messages to nil.

This is not actually a new information. It is quite old. However, it was new to me. So, good thing about LLVM compiler is that now even C struct‘s are returned initialized to 0 when you send message to nil.

Here is what returned when you call method on nil instance:

// when you call these methods on nil instance:

// any integer return type - will return 0

// any float return type - will return 0.0f

// any object return type - will return nil

// any struct return type - will return struct initialized to {0}

So, starting with Xcode 4.2 and LLVM 3.0 it is safe to assume that C struct will be zeroed out when returned from nil instance method.

Check Greg’s post for details on other return types.

How to get internals of Objective-C class

DISCLAIMER: This post describes APIs which reference internal methods and could be considered as private API and, therefore, apps could be rejected by Apple’s App Store review team. There is even a special message from Apple to avoid using these techniques in production apps.

There are situations when you want to understand more how your code works. So, you might be interested in getting all the information about particular class, including its instance variables, private methods and so on. There is even one particular reason, you might do that – to avoid accidentally overriding internal methods.

So, let’s create a method which will describe any object. We’re looking for output similar to regular class declaration.

@interface OurClass : ParentClass <Protocols...>
    // instance variables

// properties, class and instance methods


This declaration will contain definition of all derived methods, variables, including private ones.
Continue reading

Class loading, initialization and method swizzling

Rarely iOS and OS X developers might need to do initialization before their class is used. This might be called as “static constructors” in other development paradigms. Objective-C runtime also offers similar mechanisms which we will discuss in this topic.

NSObject class defines two class methods responsible for class initialization:


Continue reading

Some fun and magic with Objective-C categories

Categories are the way you can extend existing Objective-C class without subclassing. Even without having a source code of existing class.

Using categories you can add methods to existing classes (even to Foundation and UIKit classes). And you’ll be able to use them in your app. Also, you could replace existing methods with your implementation. But that’s not the topic of this tutorial. You should consult Apple documenation on categories for more information.

For example, let’s add a method to NSString to display it in alert.

@interface NSString (Alert)



Now we need to implement this method.

@implementation NSString (Alert)

    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Debug", @"Debug title")
                      cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok button title") 
                      otherButtonTitles:nil] show];


Very simple. And now you can show any NSString as alert in your application.

- (IBAction)alertText
    [self.textField.text showInAlert];

Continue reading

Работа с данными изображения

Допустим, вам захотелось странного – получить значения цветов в каждом пикселе изображения. Например, у вас есть некая библиотека обработки изображений, написанная на C или C++, и работает она с массивами байт.

В iOS мы, как правило, манипулируем объектами UIImage. Теперь нам потребуется спуститься на уровень ниже – Core Graphics.

Core Graphics – это C-фреймворк для работы с изображениями. C-функции и типы данных с префиксом CG – это и есть Core Graphics. Так что сейчас нам придется комбинировать Objective-C и C код, но нам поможет тот факт, некоторые классы Objective-C имеют эквиваленты в C, и они преобразуются друг в друга без особых проблем.

Continue reading