"Programming with Objective-c" chapter III Working with Objects

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.