NSNumber *num = @YES; NSNumber *num = [NSNumber numberWithBool:YES]; NSLog(@”%@”,num); These two statements are same. And it prints 1.
Author: hasancanakgunduz
Convenience Constructor
NSNumber *num = [NSNumber numberWithInt:0]; You do not need to manage memory when you create object using convenience constructors. Because this object will be retained by current NSAutoreleasePool and they will be release when NSAutoReleasePool is drained/Released.
NSString literal vs alloc
You can create a NSString with literal syntax or allocation. Literal: NSString *str = @”hasancan”; Alloc: NSString *str = [[NSString alloc] initWithString:@”hasancan”]; Literal version will be stored in an area called data segment and this area never changes when application is launched. So it is treated like a constant. However if you alloc NSString, it…
Print memory address
You need to use %p to print the memory address of an object. Example: NSString *str = @”hasancan”; NSLog(@”%@ %p”,str,str); // prints hasancan 0x100002268
import protocols and superclasses
You can not forward declare the superclass. You need to import its header file. Also you have to import the protocols’ header files. For this reason, protocols need to be defined in their own file. Because if you define all protocols in one header file, when you import this file you would end up with…
Forward declaring the class
@interface A @property(nonatomic,strong) B *b; @end #import “A.h”; @interface C @property(nonatomic,strong) A *a @end In this example, C imports A and needs also B because A has a property of type B. It would be bad to import B in C so you can import B in A.h. However importing B.h in A.h would…
import Foundation vs UIKit
If you want to work with Strings, Dates, etc you need to import Foundation. If you want to work with UITableViewController, UIAlertController you need to import UIKit. If you import UIKit you do not need to import Foundation because it already imports it in the backstage.
CGRect
struct CGRect { CGPoint origin; CGSize size; }; typedef struct CGRect CGRect; CGRect is a C structure and defined as above. It uses stack space and do not have * in definition: CGRect frame; This variable does not hold Objective-C object. Objects have overhead of allocation and deallocation of heap memory, structs does not have…
Stack vs Heap
NSString *str1 = “hasancan” NSString *str2 = str1 Here str1 and str2 are variables and are allocated in the stack. They both point to same NSString object. And NSString object is allocated in the heap. Stack is automatically cleaned when the stack frame is popped. However you need to manage heap memory to clean it.
New vs alloc init
[[ClassName alloc] init] [ClassName new] Which one would you prefer to create an object? They both do the same thing. However “new” is old. “Alloc init” is favoured more so I would suggest using alloc init. Also with alloc you can use your custom init function.
Function calling vs messaging
[obj performWith:param1]; //messaging(obj-C) obj->perform(param1); //function calling(C++) Objective C uses messaging that is runtime decides which code to be executed. At function calling, compiler decided which code to be called. With polymorphism, a form of runtime lookup called virtual table was introduced to function calling. However at messaging, it is always at runtime. Most of the…
