ios - Define a section of code in a class that's configured for two targets to run only in one of the targets: -
i'm working on custom keyboard project. in project have 2 targets (the app , keyboard targets). build class that's responsible handling network data both of targets:
class feeddatamanager: urlmanagerdelegate, modelmanagerdelegate { //class variables.... func initforkeyboard() { self.mmodelmanager = modelmanager(acontext: self.managedobjectcontext()) self.mmodelmanager.delegate = self self.mmodelmanager.mdelegatehashstring = hashstring(self) self.mfeedsarray = array<news>() mmodelmanager.getfeeds(&self.mfeedsarray) self.murlmanager = urlmanager() self.murlmanager.delegate = self } func initforapp() { self.mmodelmanager = modelmanager(acontext: self.managedobjectcontext()) self.mmodelmanager.delegate = self self.mmodelmanager.mdelegatehashstring = hashstring(self) let uuid: string? = userdefaultsmanager.sharedinstance.getobjectforkey("uuid") as? string self.murlmanager = urlmanager() self.murlmanager.delegate = self murlmanager.dogetlanguagesrequest() if uuid == nil { murlmanager.doregistrationrequest() } } //mark: - news related methods func getnews() { self.mfeedsarray.removeall(keepcapacity: false) self.mmodelmanager.getfeeds(&self.mfeedsarray) logger.printlogtoconsole(tag, amethodname: __function__, amessage: "feeds array count: \(mfeedsarray.count)") self.murlmanager.dogetsourcesrequest() } func handlerequestresults(aactioncode: int, adata: nsdata? = nil) { if let val = adata { if aactioncode == kiboconstants.networkactioncodes.get_news_done { let currenttime: double = nsdate().timeintervalsince1970 userdefaultsmanager.sharedinstance.setdouble(currenttime, akey: kiboconstants.commonstrings.user_def_last_update_time_stamp) var json: anyobject! = nsjsonserialization.jsonobjectwithdata(adata!, options: .mutablecontainers, error: nil) var nextupdate: anyobject? = json.valueforkey(kiboconstants.commonstrings.user_def_next_update) self.settimewithnextupdatevalue(nextupdate) self.mmodelmanager.updatenews(json, acallerhashstring: hashstring(self)) } else if aactioncode == kiboconstants.networkactioncodes.get_sources_done { var jsonwrapped: anyobject! = nsjsonserialization.jsonobjectwithdata(adata!, options: .mutablecontainers, error: nil) if self.mmodelmanager == nil { self.mmodelmanager = modelmanager(acontext: self.managedobjectcontext()) self.mmodelmanager.delegate = self self.mmodelmanager.mdelegatehashstring = hashstring(self) } if let json: anyobject = jsonwrapped { mmodelmanager.updatesources(json) } } else if aactioncode == kiboconstants.networkactioncodes.get_languages_done { #if application var json: anyobject! = nsjsonserialization.jsonobjectwithdata(adata!, options: .mutablecontainers, error: nil) if let val: anyobject = json { let languages: anyobject = json.objectforkey(kiboconstants.jsonkeys.json_key_data_languages_lowercase)! item in languages as! array<anyobject> { applicationlanguagesmanager.sharedinstance.addlanguagetoavailablelanguagesarray(item) } } #endif } } else { self.settimewithnextupdatevalue(kiboconstants.userdefaultsvalues.default_next_news_update_val) } } ..... }
if handlerequestresults method, see section:
#if application var json: anyobject! = nsjsonserialization.jsonobjectwithdata(adata!, options: .mutablecontainers, error: nil) if let val: anyobject = json { let languages: anyobject = json.objectforkey(kiboconstants.jsonkeys.json_key_data_languages_lowercase)! item in languages as! array<anyobject> { applicationlanguagesmanager.sharedinstance.addlanguagetoavailablelanguagesarray(item) } } #endif
and in application target have defined following in preprocessor macros section:
now when run application in debug , use "po application" @ point relevant print, while in keyboard extension following error: error: <expr>:1:1: error: use of unresolved identifier 'application'
meaning configuration read correctly.
the problem/question is: in application's run code still isn't executed. knows why? missing here?
thanks in advance.
rainer's answer correct.
however, may try this. in application target add macro:
application = 1
and in keyboard extension add
application = 0
with able use current code without problems. and, additional benefit, keyboard extension won't complain cannot find definition of application , ignore code.
either approach works.
Comments
Post a Comment