objective c - Static library callback -


i have static library sending responses using nsnotificationcenter. feel delegates better solution. problem how call delegate methods when not know headers yet. im not sure how implement stuff not me when library in use.

so there of allowing user create own methods , library calling them when needed?

delegates communicated declaring protocol specifies set of callbacks.

@protocol monimagegeneratorobserver <nsobject> @required // called when image has been generated - (void)generatedimagewassavedtourl:(nsurl *)purl;  - (void)imagegenerationdidcomplete;  @end 

then can tell client interface must implement specifying protocol in apis:

- (void)setimagegeneratorobserver:(nsobject<monimagegeneratorobserver>*)pobserver; 

another way accomplish provide parameter block. you'll want copy block when receive it. don't need know client's code.

your api should specific:

  • that copy it
  • on thread block performed

@interface monobject : nsobject  + (void)performasynchronousloadwithsuccessfulcallback:(void(^)(void))psuccess errorcallback:(void(^)(nserror *))perror;  @end   @implementation monobject  + (void)performasynchronousloadwithsuccessfulcallback:(void(^)(void))psuccess errorcallback:(void(^)(nserror *))perror {  if (random()%2) {   // load succeeded!   psuccess();  }  else {   // load failed = =   nserror * e = ...;   perror(e);  } }  @end 

in both cases, you specify interface , not need see headers in implementation. can call own methods in block body or in definitions of monimagegeneratorobserver callbacks.


Comments