Message Forwarding in Objective C

前端 未结 2 1024
感情败类
感情败类 2021-01-19 10:22

Can anyone give a brief explanation of how to use message forwarding?

Links

  • Apple documentation: Apple documentation tends to be good as a reference, b
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 10:33

    A common use of message forwarding is to make a class act as a proxy for other classes: you send a message to an instance of this NSProxy subclass, and it dispatches it to whichever class or object it deems fit.

    Message forwarding really just allows a class to receive messages that it was not designed to accept: you can even use it to dynamically create methods on the fly. An application of this would be a NSManagedObject category that let you access Core Data properties in method calls, without writing custom NSManagedObject subclasses for every entity. This sort of reminds me of method_missing in Ruby.

提交回复
热议问题