《Programming with Objective-C》第三章 Working with Objects

Object和普通变量的区别

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
}

函数f中,a指向的内存在栈中,函数退出的时候变量a将不能再被访问,其内存也会被释放;s指向的内存在堆中,函数退出的时候s也不能再被访问,但是s指向的内存可能继续存在。

 

Factory Method v.s. Abstract Factory

 Todo 等查找资料后再补上

 

Objective-C是一门动态语言

id someObject = @"Hello, World!";
[someObject removeAllObjects];

编译时,someObject是一个id类型,所以编译器不会报错。
运行时,编译器会出现Runtime Error因为NSString对象不能响应removeAllObjects.

NSString *someObject = @"Hello, World!";
[someObject removeAllObjects];

编译时,编译器知道someObject是一个NSString类型,NSString对象不能响应removeAllObjects,所以编译时编译器会报错

 

等于/不等于

//基本类型
if (someInteger == 42) 
{
    // someInteger has the value 42
}

//比较是否是同一个对象
if (firstPerson == secondPerson)    
{
    // firstPerson is the same object as secondPerson
}

//比较2个对象的内容是否相等
if ([firstPerson isEqual:secondPerson])     
{
    // firstPerson is identical to secondPerson
}

//NSNumber, NSString and NSDate等类型比较大小不能用>、<,应该用compare:方法
if ([someDate compare:anotherDate] == NSOrderedAscending) 
{
    // someDate is earlier than anotherDate
}

 

你可能感兴趣的:(《Programming with Objective-C》第三章 Working with Objects)