!!!Obj-c on Mac --- Chapter 2 ~ 4

Chapter 2 Extensions to C

Xcode uses the .m extension to indicate a file that holds Objective- C code and will be processed by the Objective- C compiler. File names ending in .c are handled by the C compiler, and .cpp files are the province of the C++ compiler. (In Xcode, all this compiling is handled by the GNU Compiler Collection [GCC], a single compiler that understands all three variations of the language.)

That Wacky #import Thing

#import is a feature provided by the GCC compiler, which is what Xcode uses when you’re compiling Objective- C, C, and C++ programs. #import guarantees that a header file will be included only once, no matter how many times the #import directive is actually seen for that file.

  • In C, programmers typically use a scheme based on the #ifdef directive to avoid the situation where one file includes a second file, which then, recursively, includes the first.
  • In Objective- C, programmers use #import to accomplish the same thing.

A framework is a collection of parts—header files, libraries, images, sounds, and more—collected together into a single unit.

Cocoa consists of a pair of frameworks, Foundation and Application Kit (also known as AppKit), along with a suite of supporting frameworks, including Core Animation and Core Image, which add all sorts of cool stuff to Cocoa.

The Foundation framework handles features found in the layers beneath the user interface, such as data structures and communication mechanisms.

Each framework has a master header file that includes all the framework’s individual header files. By using #import on the master header file, you have access to all the framework’s features.

When you include the master header file with #import <Foundation/Foundation.h>, you get all Foundation header files.

NSLog() and @”strings”

NSLog() is a Cocoa function that works very much like printf().

Cocoa prefixes all its function, constant, and type names with “NS”. The prefix helps prevent name collisions

A string in double quotes preceded by an at sign means that the quoted string should be treated as a Cocoa NSString element.

#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSLog (@"Hello, Objective- C!");
return (0);
} // main

Are You the Boolean Type?

BOOL in Objective- C is actually just a type definition (typedef) for the signed character type (signed char), which uses 8 bits of storage. YES is defined as 1 and NO as 0 (using #define).


Objective-C doesn’t treat BOOL as a true Boolean type that can hold only YES or NO values. The compiler considers BOOL to be an 8- bit number, and the values of YES and NO are just a convention. This causes a subtle gotcha: if you inadvertently assign an integer value that’s more than 1 byte long, such as a short or an int value, to a BOOL variable, only the lowest byte is used for the value of the BOOL. If that byte happens to be zero (as with 8960, which in hexadecimal is 0x2300), the BOOL value will be zero, the NO value.

// Correct Code
BOOL areIntsDifferent (int thing1, int thing2)
{
    if (thing1 == thing2) {
        return (NO);
    } else {
        return (YES);
    }
} // areIntsDifferent

BOOL areIntsDifferent_faulty (int thing1, int thing2)
{
return (thing1 - thing2);
} // areIntsDifferent_faulty

if(areIntsDifferent_faulty(8, 25) == YES)         // the return value is not equal to YES
if(areIntsDifferent_faulty(8,25))                 // this is safe, as NO is always 0
when you print the values of arbitrary objects with NSLog(), you’ll use the %@ format specification. When you use this specifier, the object supplies its own NSLog() format via a method named description. The description method for NSString simply prints the string’s characters.

Chapter 3 Introduction to Object- Oriented Programming

Indirection Through Filenames

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    const char *words[4] = { "aardvark", "abacus",
                            "allude", "zygote" };
    int wordCount = 4;
    int i;
    for (i = 0; i < wordCount; i++) {
        NSLog (@"%s is %d characters long", words[i], strlen(words[i]));
    }
    return (0);
} // main
We use %s, because words is an array of C strings rather than of @"NSString" objects.

OOP in Objective- C

It’s important to remember that the colon is a very significant part of the method’s name. The method

- (void) scratchTheCat;
is distinct from

- (void) scratchTheCat: (CatType) critter;
A common mistake made by many freshly minted Objective-C programmers is to indiscriminantly add a colon to the end of a method name that has no arguments. In the face of a compiler error, you might be tempted to toss in an extra colon and hope it fixes things. The rule to follow is this: If a method takes an argument, it has a colon. If it takes no arguments, it has no colons.

The @interface section, which we just discussed, defines a class’s public interface. The interface is often called theAPI, which is a TLA for “application programming interface” (and TLA is a TLA for “three- letter acronym”).

You might think that defining a method solely in the @implementation directive makes it inaccessible from outside the implementation, but that’s not the case. Objective- C doesn’t really have private methods. There is no way to mark a method as being private and preventing other code from calling it. This is a side effect of Objective- C’s dynamic nature.

Because an object’s local variables are specific to that instance of the object, we call them instance variables, often shortened to “ivars.”

Chapter 4 Inheritance

method dispatching

NSObject declares one instance variable, called isa, which holds the pointer to the object’s class.

Instance Variable

Every method call gets a hidden parameter, called self, which is a pointer to the object that receives the message.

self points to the first instance variable of the first class in the chain of inheritance.

The compiler works its magic by using a “base plus offset” mechanism. Given the base address of an object— that is, the memory location of the first byte of the first instance variable—the compiler can find all other instance variables by adding an offset to that address.

For example, if the base address of the rounded rectangle object is 0x1000, the isa instance variable is at 0x1000 + 0, which is 0x1000. isa is a 4- byte value, so the next instance variable, fillColor, starts at an offset of four, at 0x1000 + 4, or 0x1004. Every instance variable has an offset from the object’s base.
When you access the fillColor instance variable in a method, the compiler generates code to take the value that self holds and add the value of the offset (4, in this case) to point to the location where the variable’s value is stored.
This does lead to problems over time. These offsets are now hard- coded into the program generated by the compiler. Even if Apple’s engineers wanted to add another instance variable to NSObject, they couldn’t, because that would change all of the instance variable offsets. This is called the fragile base class problem.
Apple has fixed this problem with the new 64- bit Objective- C runtime introduced with Leopard, which uses indirection for determining ivar locations.

Overriding Methods

super vs. self              P70

When you override a method, invoking the superclass method is almost always a good idea, in case it’s doing more work than you’re aware of.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值