heres scenario. of network calls need have api key inserted header field when making request. thinking make category of nsmutableurlrequest. override 1 of initalizers. in 1 initializer set api key header field. everytime create object of nsmutableurlrequest header field need set. if @ apple doc here nsmutableurlrequest can see object has 4 initializers 2 class , 2 instance methods. ill list questions
- what initializer function should override in order complete task? class or instance ones?
- how can override it? either if instance or class initializer?
- is way go it? or should subclass , override that?
my code has been written sometime , dont want go , insert api key each individual request because theres lot of them. in way think better approach because ill have set apikey in 1 place , not many place, lessens probability of programming errors.thank help.
p.s. if isnt way complete can still show me how class initializer works? whats underlying code can generate own static class initializers also. each time try override class method cant figure out type return.
thank help
categories not meant used override. use categories when want add additional functionality existing classes. if want override initializers use inheritance.
the static functions not initializers, helper functions creates instance you, there's no need handle them. simple initwithurl:
method simplified version provide default values designated initializer initwithurl:cachepolicy:timeoutinterval:
; in subclass need override single initializer, looking this:
@interface mynsmutableurlrequest : nsmutableurlrequest @end @implementation mynsmutableurlrequest - (instancetype)initwithurl:(nsurl *)url cachepolicy:(nsurlrequestcachepolicy)cachepolicy timeoutinterval:(nstimeinterval)timeoutinterval { self = [super initwithurl:url cachepolicy:cachepolicy timeoutinterval:timeoutinterval]; if (self) { //do stuff here } return self; }
Comments
Post a Comment