Objective C delegate or C-style block callback?

前端 未结 3 1393
花落未央
花落未央 2021-02-09 02:46

I am designing a class that will \"fire events\" whenever something occurs. These events tend to be non-UI related. I\'m wondering what the best method for doing so is. I\'ve be

3条回答
  •  死守一世寂寞
    2021-02-09 03:19

    Each has its use.

    Delegates should be used when there are multiple "events" to tell the delegate about and/or when the class needs to get data from the delegate. A good example is with UITableView.

    A block is best used when there is only one (or maybe two) event. A completion block (and maybe a failure block) are a good example of this. A good example is with NSURLConnection sendAsynchronousRequest:queue:completionHandler:.

    A 3rd option is notifications. This is best used when there are possibly multiple (and unknown) interested parties in the event(s). The other two are only useful when there is one (and known) interested party.

提交回复
热议问题