ios - How to structure one to many to many to many data structure Swift -
i'm having hard time figuring out how best map out data structure takes user, adds brands associated user, adds items of clothing , user's size (for brand's item of closing, user).
class user { var name: string class brands : user { var brandname: [string] class itemandsize : brands { var itemandsize: [string: int] } } }
is there better way structure in swift? want able store , call user.brandname.itemandsize.
the best not create relationship using inheritance. relationship between people, objects , specs can solved mapping in table. in example below, separated 'item' 'sizing' because dictionary won't map user brand , item/sizing unless compose key concatenating primary keys:
struct user { var id: int var description: string } struct brand { var id: int var description: string } enum sizing { case s, m, l, xl var description : string { switch self { case .s: return "s"; case .m: return "m"; case .l: return "l"; case .xl: return "xl"; } } } enum clothing { case pants, shorts, shirt, jacket var description : string { switch self { case .pants: return "pants"; case .shorts: return "shorts"; case .shirt: return "shirt"; case .jacket: return "jacket"; } } } struct mapping { var user: user var brand: brand var item: clothing var size: sizing } let users = [user(id: 1, description: "pierre"), user(id: 2, description: "paul"), user(id: 3, description: "jacques")] let brands = [brand(id: 1, description: "d&g"), brand(id: 2, description: "gucci"), brand(id: 3, description: "prada")] let mappings = [mapping(user: users[0], brand: brands[1], item: .pants, size: .xl), mapping(user: users[1], brand: brands[0], item: .jacket, size: .s)] m in mappings { var s = ",".join([m.user.description, m.brand.description, m.item.description, m.size.description]) println(s) }
Comments
Post a Comment