iOS Tips

This thread will be updated every time I find some usefull quick tip. It is usually great idea to have some notepad to write some tips so you can later access it when you need. I will be creating sections when needed.



1. One can verify plist file, if the structure is right by using

 plutil myfile.plist






1. Enabling debbuging of all messages that are sent in our application.





Then look at /tmp/msgSends-<pid> file 

Please note: This only works in simulator.


UIViews / UIViewControllers


1. One can create mask for UIView so some of the content might be hidden.



	CALayer *mask = [CALayer layer];
	mask.backgroundColor = [UIColor blackColor].CGColor;
	mask.frame = CGRectMake(0, 0, 20, 20);
	view.layer.mask = mask;

This will make view transparent on top left corner. 

We can use different colors or even UIImage when set to contents propery.


2. Change UIContainerView to have round corners like popover on iPad.


 [[self.sortContainerView layer] setCornerRadius:10.0f];
 [[self.sortContainerView layer] setBorderColor:[UIColor lightGrayColor].CGColor];
 [[self.sortContainerView layer] setBorderWidth:1.5f];
 [[self.sortContainerView layer] setShadowColor:[UIColor blackColor].CGColor];
 [[self.sortContainerView layer] setShadowOpacity:0.8];
 [[self.sortContainerView layer] setShadowRadius:3.0];
 [[self.sortContainerView layer] setShadowOffset:CGSizeMake(2.0, 2.0)];
 [[self.sortContainerView layer] setMasksToBounds:YES]; 

3. In iOS6 shouldAutorotate is not passed from UINavigationController or UITabBarController

Fix is to implement subclass of them and add:



- (BOOL)shouldAutorotate
 return [self.selectedViewController shouldAutorotate];


- (BOOL)shouldAutorotate
 return [self.visibleViewController shouldAutorotate];


1. Lazy instantiation

Basically object is initialized when we use it instead of when application starts.

	@interface SomeClass : NSObject
	@property (strong, nonatomic) NSMutableDictionary *dictionary;

	@implementation SomeClass
	@synthesize dictionary = _dictionary;

	- (NSMutableDictionary) dictionary {
	 if (!_dictionary) {
		dictionary = [[NSMutabledictionary alloc] init];
	 return _dictionary;

2. #pragma mark

It helps to organize your code in navigation bar of Xcode. You can group many function together making it clear. For instance:

#pragma mark - // will enter horizontal separator

#pragma mark Initialization methods // will enter Initialization methods before actuall methods

3. Cool replacement for CGRectMake

 self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};

 4. Compile non arc code under ARC project

Add -fno-objc-arc flag to Build Phases for files you want to skip arc (tip: shift + enter when selecting multiple files)

5. Adding object to NSArray not retaining it.  

Adding: [yourArray addObject:[NSValue valueWithNonretainedObject:yourObject]]

Retrieving: [[yourArray objectAtIndex:index] nonretainedObjectValue]

Core data

1. How to create temporary core data 's NSManagedObject

First is to create temporary NSManagedObjectContext then set persistent store to be the same as withing original context. After that you can use this temp context, and when you want to save, you do so and then you need to merge it with your main context with after receiving save notification from temp context.


NSManagedObjectContext *tmpContext = [[[NSManagedObjectContext alloc] init] autorelease];
[tmpContext setPersistentStore:[origContext persistentStore]];

// register for save notification
[[NSNotificationCenter defaultCenter] addObserver:self
// notification itself
- (void)tmpContextSaved:(NSNotification *)notification {
 dispatch_async(dispatch_get_main_queue(), ^{
     [originalContext mergeChangesFromContextDidSaveNotification:notification];

// save tmpContext 

[tmpContext save:NULL];

// remove tmpContext notification observing
[[NSNotificationCenter defaultCenter] removeObserver:self