Objective-C object messaging

发布时间:2014-10-23 23:25:25
来源:分享查询网

1. To summarize, the following are the key elements of Objective-C object messaging: Message: A name (the selector) and a set of parameters sent to an object/class.Method: An Objective-C class or instance method that has a specific declaration comprised of a name, input parameters, a return value, and the method signature (the data type(s) for the input parameters and return value).Method binding: The process of taking a message sent to a particular receiver and finding and executing the appropriate method. The Objective-C runtime performs dynamic binding of messages to method calls. 2. Selectors In Objective-C object messaging, a selector is a text string that refers to a method and can be sent to an object or a class. The Objective-C runtime uses selectors to retrieve the correct method implementation for a target object/class. A selector is represented as a text string broken up into segments, with a colon placed at the end of each segment that is followed by a parameter: nameSegment1:nameSegment2:nameSegment3: 3. The SEL Type Up to this point, you have defined a selector as a text string that is part of a message in a message-passing expression; now, you’ll examine the selector type. A selector type (SEL) is a special Objective-C type that represents a unique identifier that replaces a selector value when the source code is compiled. All methods with the same selector value have the same SEL identifier. The Objective-C runtime system ensures that each selector identifier is unique. A variable of type SEL can be created using the @selector keyword. SEL myMethod = @selector(myMethod); So why would you create SEL variables? Well, the Objective-C runtime system (via NSObject) includes many methods that utilize variables of type SEL as parameters to dynamic methods. In addition to obtaining information about objects and classes, NSObject includes several methods for invoking a method on an object using a selector parameter. The following example uses the NSObjectinstance method performSelector:withObject:withObject: to invoke a method specified by the selector variable. [myCalculator performSelector:@selector(sumAddend1::) withObject:[NSNumber numberWithInteger:25]                          withObject:[NSNumber numberWithInteger:10]]; The @selector directive creates a selector variable at compile time. You can also create a selector at runtime with the Foundation Framework NSSelectorFromString function. In this case, the previous example is updated as follows:  SEL selector = NSSelectorFromString(@"sumAddend1::");  [myCalculator performSelector:selector withObject:[NSNumber numberWithInteger:25]                          withObject:[NSNumber numberWithInteger:10]]; 4. NSStringFromSelector(_cmd) The input parameter for this function is a variable of type SEL. So what’s this _cmd parameter and where did it come from? Well, _cmd is an implicit parameter (available in the implementation of every Objective-C method but not declared in its interface) that holds the selector of the message being sent. Thus, the expression NSStringFromSelector(_cmd) returns a text string for the selector of the method being invoked.

返回顶部
查看电脑版