The difference between Object and ordinary variables
If you ’re used to using terms like the stack and the heap, a local variable is allocated on the stack, while objects are allocated on the heap.
-(void) f
{
int a = 2; // Stack
NSString * s = @ "Hello"; // Heap
}
In the function f, the memory pointed to by a is on the stack. When the function exits, the variable a can no longer be accessed, and its memory is also released; the memory pointed to by s is in the heap, and when the function exits, s can no longer be accessed. But the memory pointed to by s may continue to exist.
Factory Method v.s. Abstract Factory
Todo waits to find the information before filling in
Objective-C is a dynamic language
id someObject = @ "Hello, World!";
[someObject removeAllObjects];
When compiling, someObject is an id type, so the compiler will not report an error.
When running, the compiler will have a Runtime Error because NSString objects cannot respond to removeAllObjects
NSString * someObject = @ "Hello, World!";
[someObject removeAllObjects];
When compiling, the compiler knows that someObject is an NSString type, and NSString objects cannot respond to removeAllObjects, so the compiler will report an error when compiling
Equal / not equal
//basic type
if (someInteger == 42)
{
// someInteger has the value 42
}
// Compare if it is the same object
if (firstPerson == secondPerson)
{
// firstPerson is the same object as secondPerson
}
// Compare whether the contents of 2 objects are equal
if ([firstPerson isEqual: secondPerson])
{
// firstPerson is identical to secondPerson
}
// NSNumber, NSString and NSDate and other types can not be compared using the size>, <, should use compare:
if ([someDate compare: anotherDate] == NSOrderedAscending)
{
// someDate is earlier than anotherDate
}
Chapter 3 of Working with Objects in Programming with Objective-C