一、
A Foundation Tool has no graphical user interface and typically runs on the command line or in the background as a daemon. Unlike in an application project,you will always alter the main function of a Foundation Tool.
In the toolbar,you will see a pop-up for the Active Build Configuration. There are two choices: DeBUG and Release. While working on your application,you will always want to be in DeBUG. Before you ship your application,you will do a Release build. How are they different? A Release build is universal and has had its deBUGging symbols stripped. Thus,the Release build takes about twice as long to compile and has none of the stuff that the deBUGger needs to do its job.
二、Sending Messages to nil
In most object-orIEnted languages,your program will crash if you send a message to nil. In applications written in those languages,you will see many checks for nil before sending a message. In Java,for example,you frequently see the following:
if (foo != null) { foo.doThatThingYouDo();}
In Objective-C,it is okay to send a message to nil. The message is simply discarded,which eliminates the need for these sorts of checks. For example,this code will build and run without an error:
ID foo;foo = nil;int bar = [foo count];This approach is different from how most languages work,but you will get used to it.
You may find yourself asking over and over,"Argg! Why isn't this method getting called?" Chances are,the pointer you are using,assuming it is not nil,is actually nil.
In the preceding example,what is bar set to? Zero. If bar were a pointer,it would be set to nil (zero for pointers). For other types,the value is less predictable.
NSObject is the root of the entire Objective-C class hIErarchy. Some commonly used methods on NSObject are described next.
四、How Does Messaging Work?- (ID)init
Initializes the receiver after memory for it has been allocated. An init message is generally coupled with an allocmessage in the same line of code:
TheClass *newObject = [[TheClass alloc] init];
- (Nsstring *)description
Returns an Nsstring that describes the receiver. The deBUGger's print object command ("po") invokes this method. A good description method will often make deBUGging easIEr. Also,if you use %@ in a format string,the object that should be substituted in is sent the message description. The value returned by the description method is put into the log string. For example,in your main function,the line
NSLog(@"The number at index %d is %@",i,numbertoprint);
is equivalent to
- (BOol)isEqual:(ID)anObject
Returns YES if the receiver and anObject are equal and NO otherwise. You might use it like this:
if ([myObject isEqual:anotherObject]) { NSLog(@"They are equal.");}
But what does equal really mean? In NSObject,this method is defined to return YES if and only if the receiver andanObject are the same object—that is,if both are pointers to the same memory location.
Clearly,this is not always the equal that you would hope for,so this method is overrIDden by many classes to implement a more appropriate IDea of equality. For example, Nsstring overrIDes the method to compare the characters in the receiver and anObject. If they have the same characters in the same order,the two strings are consIDered equal.
Thus,if x and y are Nsstrings,there is a big difference between these two Expressions:
x == y
and
[x isEqual:y]
The first Expression compares the two pointers. The second Expression compares the characters in the strings. Note,however,that if x and y are instances of a class that has not overrIDden NSObject's isEqual: method,the two Expressions are equivalent.
As mentioned earlIEr,an object is like a C struct. NSObject declares an instance variable called isa. Because NSObject is the root of the entire class-inheritance tree,every object has an isa pointer to the class structure that created the object (Figure 3.15). The class structure includes the names and types of the instance variables for the class,as well as the implementation of the class's methods. The class structure has a pointer to the class structure for its superclass.
The methods are indexed by the selector. The selector is of type SEL. Although SEL is defined to be char *int. Each method name is mapped to a unique int. For example,the method name addObject: might map to the number 12. When you look up methods,you will use the selector,not the string @"addobject:".
As part of the Objective-C data structures,a table maps the names of methods to their selectors. Figure 3.16 shows an example.
figure 3.16. The Selector table
At compile time,the compiler looks up the selectors wherever it sees a message send. Thus,
[myObject addobject:yourObject];becomes (assuming that the selector for addobject: is 12)
objc_msgSend(myObject,12,yourObject);Here,monospace">objc_msgSend() looks at myObject's isa pointer to get to its class structure and looks for the method associated with 12. If it does not find the method,it follows the pointer to the superclass. If the superclass does not have a method for 12,it continues searching up the tree. If it reaches the top of the tree without finding a method,the function throws an exception.
Clearly,this is a very dynamic way of handling messages. These class structures can be changed at runtime. In particular,using the NSBundle class makes it relatively easy to add classes and methods to your program while it is running. This very powerful technique has been used to create applications that can be extended by other developers.
总结以上是内存溢出为你收集整理的Cocoa Programming for Mac OS X 第三章(Objective-C)摘录全部内容,希望文章能够帮你解决Cocoa Programming for Mac OS X 第三章(Objective-C)摘录所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)