Hello, I am an absolute newbie to swift and writing my first iPhone app and I can't figure out how to store my data within my program before I commit it to persistant storage.
My program needs to hold lots of structured data entered by the user during program execution before it is committed to persistent storage (on a remote database).
I have 3 Structs (since I thought this is the best path to go down), TopLevelStruct, MidLevelStruct and BottomLevelStruct and the latter 2 structs are nested arrays within their immediate predecessor. (note there may be typos in this code...please ignore...I'm just trying to understand how to structure my program). I want the ability to reset the array index in the bottom structure for each instance of the mid structure. See below
struct bottomLevelStruct {
var bottomNum: Int
var bottomDescription: String
var bottomResult: String
}
struct MidLevelStruct{
var midNum: Int
var midTotal: Int
var bottomLevelDetails = [bottomLevelStruct] ()
}
struct TopLevelStruct{
var date: String
var total: Int
var midLevelDetails = [MidLevelStruct] ()
}
// declare the bottom level struct and set some data
var myBottomLevelStructArray = [bottomLevelStruct(bottomNum: 1, bottomDescription: "something", bottomResult: "Failed")]
myBottomLevelStructArray.append(bottomLevelStruct(bottomNum: 1, bottomDescription: "2nd thing", bottomResult: "Success"))
/ / declare the mid level struct
var myMidLevelStructArray = [MidLevelStruct.init(midNum: 1, midTotal: 3, bottomLevelDetails: myBottomLevelStructArray)]
/ / and the top level struct
var myTopLevelStruct = TopLevelStruct.init(date: "today", total: 22, myTopLevelStruct: midLevelDetails)
/ / now add the data to the bottom struct that will be associated with the 2nd mid level array element
myBottomLevelStructArray.append(bottomLevelStruct(bottomNum: 2, bottomDescription: "New thing", bottomResult: "OK"))
myBottomLevelStructArray.append(bottomLevelStruct(bottomNum: 2, bottomDescription: "Wild thing", bottomResult: "Success"))
/ / append the data to the mid level array
myMidLevelStructArray.append = (MidLevelStruct.init(midNum: 2, midTotal: 3, bottomLevelDetails: myBottomLevelStructArray)
And here is where my problem begins. Although I can access the mid level data by array index such as (these are ok):
myMidLevelStructArray[0].midNum //and
myMidLevelStructArray[0].bottomLevelDetails[0]
myMidLevelStructArray[1].midNum
However, I want to be able to access the bottom level details in subsequent midLevelArrays starting at index 0. Right now the 1st bottom level element of the 2nd mid array is:
myMidLevelStructArray[1].bottomLevelDetails[2]
I want to be able to access this as:
myMidLevelStructArray[1].bottomLevelDetails[0]
since the number of bottomLevel details has a 1 to many relationship with the mid level structure.
Can this be done using this structure or do I need to restructure my code in another manner. Sorry for the verbose entry....couldn't figure out another way of explaning it.
Thank you.