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:

-(NSInteger)intMethod;
// any integer return type - will return 0

-(float)floatMethod;
// any float return type - will return 0.0f

-(id)objectMethod;
// any object return type - will return nil

-(CGRect)structMethod;
// 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

@end

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

Apple’s hidden message to developers

While preparing source code for one of the next posts, I’ve found nice piece of Apple’s humour.

UIViewController class has this private method declared: -attentionClassDumpUser:yesItsUsAgain:althoughSwizzlingAndOverridingPrivateMethodsIsFun:itWasntMuchFunWhenYourAppStoppedWorking:pleaseRefrainFromDoingSoInTheFutureOkayThanksBye:

I would totally agree with them, don’t mess with internal methods.

P.S. There also some leftovers from bugfixing, I suppose: -_hackFor11408026_beginAppearanceTransition:animated: and
-_hackFor11408026_endAppearanceTransition.

Who knows, what is behind rdar://11408026?

How to pass a document from one app to another

This post will cover some of interoperability questions in iOS. Many business apps deal with documents (or files). Sandboxing approach of iOS prevents app from just browsing the file system and looking for files. However, sometimes app just needs to be able to pass file to another app. There is an app API for that.

Let’s see what do we need to cover these tasks:

  1. Registering our app as file handler
  2. Handling files passed from other apps
  3. Passing file from our app to other apps
  4. (optional) Previewing files

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:

+(void)load;
+(void)initialize;

Continue reading

Better way of handling network activity indicator

Since iOS 2.0 application could control network activity indicator in the status bar. Responsible application should show it when network access is in progress and hide when all network activities are done.
Network Activity Indicator
This could be achieved by using networkActivityIndicatorVisible property of UIApplication.

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// do some network stuff
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

This approach works great if you have only one place where network access is possible. And there is only one thread.

But we’re living in a very complex world and, usually, we could have several threads accessing networks. So we can’t just show and hide indicator. We have to show it when first thread starts network activities and hide when last thread stops activities.
Continue reading