iOS Developer Library

Developer

Foundation Framework Reference NSMethodSignature Class Reference

Options
Deployment Target:

On This Page
Language:

NSMethodSignature

An NSMethodSignature object records type information for the return value and parameters of a method. It is used to forward messages that the receiving object does not respond to—most notably in the case of distributed objects.

You typically create an NSMethodSignature object using the NSObject methodSignatureForSelector: instance method (on OS X 10.5 and later you can also use signatureWithObjCTypes:). It is then used to create an NSInvocation object, which is passed as the argument to a forwardInvocation: message to send the invocation on to whatever other object can handle the message. In the default case, NSObject invokes doesNotRecognizeSelector:, which raises an exception. For distributed objects, the NSInvocation object is encoded using the information in the NSMethodSignature object and sent to the real object represented by the receiver of the message.

Type Encodings

An NSMethodSignature object is initialized with an array of characters representing the string encoding of return and argument types for a method. You can get the string encoding of a particular type using the @encode() compiler directive. Because string encodings are implementation-specific, you should not hard-code these values.

A method signature consists of one or more characters for the method return type, followed by the string encodings of the implicit arguments self and _cmd, followed by zero or more explicit arguments. You can determine the string encoding and the length of a return type using methodReturnType and methodReturnLength properties. You can access arguments individually using the getArgumentTypeAtIndex: method and numberOfArguments property.

For example, the NSString instance method containsString: has a method signature with the following arguments:

  1. @encode(BOOL) (c) for the return type

  2. @encode(id) (@) for the receiver (self)

  3. @encode(SEL) (:) for the selector (_cmd)

  4. @encode(NSString *) (@) for the first explicit argument

See Type Encodings in Objective-C Runtime Programming Guide for more information.

  • Returns an NSMethodSignature object for the given Objective-C method type string.

    Declaration

    Objective-C

    + (NSMethodSignature *)signatureWithObjCTypes:(const char *)types

    Parameters

    types

    An array of characters containing the type encodings for the method arguments.

    Return Value

    An NSMethodSignature object for the given Objective-C method type string in types.

    Availability

    Available in iOS 2.0 and later.

  • Returns the type encoding for the argument at a given index.

    Declaration

    Objective-C

    - (const char *)getArgumentTypeAtIndex:(NSUInteger)index

    Parameters

    index

    The index of the argument to get.

    Return Value

    The type encoding for the argument at index.

    Discussion

    Indices begin with 0. The implicit arguments self (of type id) and _cmd (of type SEL) are at indices 0 and 1; explicit arguments begin at index 2.

    Availability

    Available in iOS 2.0 and later.

  • The number of arguments recorded in the receiver. (read-only)

    Declaration

    Objective-C

    @property(readonly) NSUInteger numberOfArguments

    Discussion

    There are always at least two arguments, because an NSMethodSignature object includes the implicit arguments self and _cmd, which are the first two arguments passed to every method implementation.

    Availability

    Available in iOS 2.0 and later.

  • frameLength Property

    The number of bytes that the arguments, taken together, occupy on the stack. (read-only)

    Declaration

    Objective-C

    @property(readonly) NSUInteger frameLength

    Discussion

    This number varies with the hardware architecture the application runs on.

    Availability

    Available in iOS 2.0 and later.

  • A C string encoding the return type of the method in Objective-C type encoding. (read-only)

    Declaration

    Objective-C

    @property(readonly) const char *methodReturnType

    Availability

    Available in iOS 2.0 and later.

  • The number of bytes required for the return value. (read-only)

    Declaration

    Objective-C

    @property(readonly) NSUInteger methodReturnLength

    Availability

    Available in iOS 2.0 and later.

    See Also

    methodReturnType

  • Whether the receiver is asynchronous when invoked through distributed objects.

    Declaration

    Objective-C

    - (BOOL)isOneway

    Return Value

    YEStrue if the receiver is asynchronous when invoked through distributed objects, otherwise NOfalse.

    Discussion

    If the method is oneway, the sender of the remote message doesn’t block awaiting a reply.

    Availability

    Available in iOS 2.0 and later.