ios - Class or Struct for a model similar to a relational database? -
the application in question built follows:
- a user selects job
- the job can have many components
- each component can have many lineitems.
i not clear on how should structured - should class or structs? seeing 1 job being processed @ time, confident jobs should class. however, when there multiples of object type, not clear on how form them, components , lineitem objects.
the application consists of viewcontrollers , tableviewcontrollers. data fetched server in json , populated appropriate view needed. here object types setup:
a job object:
// job object // public struct job { static var globalid : string? static var name : string? static var status : string? static var client = client() static var components = array<component>() // etc.. }
a component so:
// jobcomponent object // public struct component { var name:string? = "" var fmrecordid : string? var startts:nsdate? var endts:nsdate? var notes:string? = "" var items = array<lineitem>() // etc... }
and finally, lineitem:
// lineitem object // public struct lineitem { var fmrecordid = string() var itemname = string() var itemnumber = string() // etc... }
all of these object built within public class
called "pl".
when user selects lineitem , edits it's values, values not available outside vc in edited because vc isn't referencing lineitem passed, copying it. same happens components.
a workaround found use the job struct pl.job.self
, modify components , lineitems i
= desired index in array:
pl.job.components[i]
access componentpl.job.components[i].items[i]
access specific item within component.
however, doesn't scale well.
the desired behavior able pass reference particular instance of object around rather pass around index path of objects in pl.job
object.
i aware there wrong how structured, please point me in right direction?
a couple of points:
you can pass class instances reference. if want able pass reference particular
lineitem
orcomponent
orjob
, , want able make changes object effective everywhere, need define them classes , not structs. instances of struct types always passed value , not reference. , when value type passed, copied, meaning create entirely new copy of object, , mutating copy has no effect on original.your
job
struct has static properties - i.e., there ever 1globalid
,name
,status
etc. throughout entire application. if want have multiple instances ofjob
, these should not static properties. 1job
processed @ time, maybe intentional. either way, still preferable create instance ofjob
class has properties. give more flexibility later if decide make possible hold references multiple jobs in memory, or allow user select between different jobs, or switch between jobs, etc. example, may want allow user switchjob
processing earlier without destroyingjob
working on now.
but think main point need define objects classes if want able pass them reference. if modify object passed reference, other references same object show same changes (because, after all, references same object). doesn't work value types, structs.
Comments
Post a Comment