ios - Expected Type in Protocol -
i implemented protocol in objective-c , when use own class type definition xcode tells type required.
#import <foundation/foundation.h> #import <uikit/uikit.h> #import "lprphotocapturecamera.h" @protocol lprphotocapturecameradelegate <nsobject> - (void)camera:(lprphotocapturecamera *)camera finishedcapturingphoto:(uiimage *)captureduiimage; @end i looked in header files apples delegates , edited protocol @class lprphotocapturecamera , works.
#import <foundation/foundation.h> #import <uikit/uikit.h> #import "lprphotocapturecamera.h" @class lprphotocapturecamera; @protocol lprphotocapturecameradelegate <nsobject> - (void)camera:(lprphotocapturecamera *)camera finishedcapturingphoto:(uiimage *)captureduiimage; @end i wonder why xcode not throw error uiimage here, have explanation this?
what happened (i guessing because did not show files) lprphotocapturecamera.h imports lprphotocapturecameradelegate.h @ top of file. have import cycle.
so example, when compiling lprphotocapturecamera.m, import lprphotocapturecamera.h, imports lprphotocapturecameradelegate.h @ top of file (before declaration of lprphotocapturecamera class). lprphotocapturecameradelegate.h imports lprphotocapturecamera.h, #import guarantees file imported once, import doesn't import anything. when gets inside declaration of lprphotocapturecameradelegate protocol, refers type lprphotocapturecamera *, doesn't understand because lprphotocapturecamera hasn't been declared yet.
in other words, although lprphotocapturecameradelegate.h imports lprphotocapturecamera.h, import doesn't import lprphotocapturecamera because have started importing (and in middle of) lprphotocapturecamera.h @ higher level.
the usual way deal when types refer each other in cycle use forward declarations. declaration of lprphotocapturecameradelegate uses pointer lprphotocapturecamera, doesn't need declaration of lprphotocapturecamera class -- needs know it's class. forward declaration @class lprphotocapturecamera do, allow no longer import lprphotocapturecamera.h in lprphotocapturecameradelegate.h. similarly, if declaration of lprphotocapturecamera uses lprphotocapturecameradelegate in type of variable, e.g. id<lprphotocapturecameradelegate> delegate, doesn't need declaration of lprphotocapturecameradelegate -- needs know it's protocol, forward declaration @protocol lprphotocapturecameradelegate do, , don't have import lprphotocapturecameradelegate.h in lprphotocapturecamera.h. usually, time need import b's header in a's header if implements b protocol or inherits b class.
wiki
Comments
Post a Comment