iOS Developer Library

Developer

Foundation Framework Reference NSProxy Class Reference

Options
Deployment Target:

On This Page
Language:

NSProxy

Inherits From


  • NSProxy

Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later

NSProxy is an abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet. Typically, a message to a proxy is forwarded to the real object or causes the proxy to load (or transform itself into) the real object. Subclasses of NSProxy can be used to implement transparent distributed messaging (for example, NSDistantObject) or for lazy instantiation of objects that are expensive to create.

NSProxy implements the basic methods required of a root class, including those defined in the NSObject protocol. However, as an abstract class it doesn’t provide an initialization method, and it raises an exception upon receiving any message it doesn’t respond to. A concrete subclass must therefore provide an initialization or creation method and override the forwardInvocation: and methodSignatureForSelector: methods to handle messages that it doesn’t implement itself. A subclass’s implementation of forwardInvocation: should do whatever is needed to process the invocation, such as forwarding the invocation over the network or loading the real object and passing it the invocation. methodSignatureForSelector: is required to provide argument type information for a given message; a subclass’s implementation should be able to determine the argument types for the messages it needs to forward and should construct an NSMethodSignature object accordingly. See the NSDistantObject, NSInvocation, and NSMethodSignature class specifications for more information.

  • Returns a new instance of the receiving class

    Declaration

    Swift

    class func alloc() -> Self

    Objective-C

    + (id)alloc

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Returns a new instance of the receiving class

    Declaration

    Objective-C

    + (id)allocWithZone:(NSZone *)zone

    Return Value

    A new instance of the receiving class, as described in the NSObject class specification under the allocWithZone: class method.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 2.0 and later

  • Deallocates the memory occupied by the receiver.

    Declaration

    Swift

    func dealloc()

    Objective-C

    - (void)dealloc

    Discussion

    This method behaves as described in the NSObject class specification under the dealloc instance method.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

    See Also

    – finalize

  • The garbage collector invokes this method on the receiver before disposing of the memory it uses.

    Declaration

    Swift

    func finalize()

    Objective-C

    - (void)finalize

    Discussion

    This method behaves as described in the NSObject class specification under the finalize instance method. Note that a finalize method must be thread-safe.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

    See Also

    – dealloc

  • Passes a given invocation to the real object the proxy represents.

    Declaration

    Swift

    func forwardInvocation(_ anInvocation: NSInvocation)

    Objective-C

    - (void)forwardInvocation:(NSInvocation *)anInvocation

    Parameters

    anInvocation

    The invocation to forward.

    Discussion

    NSProxy’s implementation merely raises NSInvalidArgumentException. Override this method in your subclass to handle anInvocation appropriately, at the very least by setting its return value.

    For example, if your proxy merely forwards messages to an instance variable named realObject, it can implement forwardInvocation: like this:

    • - (void)forwardInvocation:(NSInvocation *)anInvocation
    • {
    • [anInvocation setTarget:realObject];
    • [anInvocation invoke];
    • return;
    • }

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Raises NSInvalidArgumentException. Override this method in your concrete subclass to return a proper NSMethodSignature object for the given selector and the class your proxy objects stand in for.

    Declaration

    Objective-C

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

    Parameters

    aSelector

    The selector for which to return a method signature.

    Return Value

    Not applicable. The implementation provided by NSProxy raises an exception.

    Discussion

    Be sure to avoid an infinite loop when necessary by checking that aSelector isn’t the selector for this method itself and by not sending any message that might invoke this method.

    For example, if your proxy merely forwards messages to an instance variable named realObject, it can implement methodSignatureForSelector: like this:

    • (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
    • {
    • return [realObject methodSignatureForSelector:aSelector];
    • }

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 2.0 and later

    See Also

    methodSignatureForSelector: (NSObject)

  • Returns a Boolean value that indicates whether the receiving class responds to a given selector.

    Declaration

    Swift

    class func respondsToSelector(_ aSelector: Selector) -> Bool

    Objective-C

    + (BOOL)respondsToSelector:(SEL)aSelector

    Parameters

    aSelector

    A selector.

    Return Value

    YEStrue if the receiving class responds to aSelector messages, otherwise NOfalse.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Returns self (the class object).

    Declaration

    Swift

    class func `class`() -> AnyClass

    Objective-C

    + (Class)class

    Return Value

    self. Because this is a class method, it returns the class object

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

    See Also

    class (NSObject)
    class (NSObject protocol)

  • A string containing the real class name and the id of the receiver as a hexadecimal number. (read-only)

    Declaration

    Swift

    var description: String { get }

    Objective-C

    @property(readonly, copy) NSString *description

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • A string containing the real class name and the id of the receiver as a hexadecimal number. (read-only)

    Declaration

    Swift

    var debugDescription: String { get }

    Objective-C

    @property(readonly, copy) NSString *debugDescription

    Discussion

    This allows this method declared by the NSObject protocol to be implemented in developer classes without causing an issue to be flagged in the iOS and Mac app stores, where otherwise it might look like the developer is overriding a private method in Foundation.

    Implementation of this method is optional, so be sure to test for its existence before attempting to invoke it.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 5.0 and later