ios - Reuse large unchanged array in multiple swift classes without use of global variable -
i'm relatively new ios development, , yet problem keep coming to. in last app made, high-school textbook definition review app, had large amount of text never changed. use of global variables frowned upon, looked alternatives singletons (how these more elegant eludes me, feels redundant), prepareforsegue (solves issue, feels there must better way) , permanent storage (makes no sense data never altered, provides central repository). naively, picked latter, implementing plist, diminishing performance.
my interpretation of use of singleton problem (in case problem stems misunderstanding):
class global { let array = ["item1","item2",...,"item1042"] }
now can access array
other classes global.array
. array in fact 3-dimensional array, don't see how change issue (would have preferred create class, many instances, felt further complicating issue readability).
aside using coredata, again seems overkill, i'm @ wits end.
i want use single, unchanged array/dictionary throughout classes, preferably without copying every class.
what proper style achieve this?
i apologise if has been answered (it seems general not have been), whatever answers found either complex level of understanding, or unhelpful.
there many ways persist data
in single app session
. but, among these, prefer create variable in appdelegate
, use in app using shared instance
. way won't need create separate singleton
or other class static
variable
or kind of global
variable
.
@uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate { let array = ["item1","item2","item1042"] //.... } class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() print((uiapplication.shared.delegate as? appdelegate)?.array) } }
also, in case want persist data
within multiple app sessions
, can use userdefaults
, core data
or file system
.
Comments
Post a Comment