Optimize putting data into struct vars

I have built my first struct and it does what I wanted it to but I am sure this is not the most optimized way to add data to it. If anyone has any opimization ideas as I will be adding more data rows and then eventually move to a db. I am studying this stuff as fast as I can and I am finding it fun and challenging.



struct CCdata {
    var id: Int?
    var org_nm: String?
    var org_cd: String?
    var tms: String?
    var tec: String?
    var serno: String?
    var mcn: String?
    var jcn: String?
    var rcvd_dt: Date?
    var cmp_dt: Date?

var firstTry:[CCdata] = [CCdata]()
var firstTrySingle = CCdata()
var secondTry = "45    HSM73    Q30    MH-60R    AHZS    167043    1V4YSQU    Q30113239    4/23/2018 0:00:00    5/2/2018 0:00:00"
let secondTry_StrArr = secondTry.components(separatedBy: "    ")
firstTrySingle.id = Int(secondTry_StrArr[0])
firstTrySingle.org_nm = secondTry_StrArr[1]
firstTrySingle.org_cd = secondTry_StrArr[2]
firstTrySingle.tms = secondTry_StrArr[3]
firstTrySingle.tec = secondTry_StrArr[4]
firstTrySingle.serno = secondTry_StrArr[5]
firstTrySingle.mcn = secondTry_StrArr[6]
firstTrySingle.jcn = secondTry_StrArr[7]
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
firstTrySingle.rcvd_dt = formatter.date(from: secondTry_StrArr[8])
firstTrySingle.cmp_dt = formatter.date(from: secondTry_StrArr[9])


print(firstTry[0].org_nm ?? "Bad Data")
firstTry[0].org_nm = "HSM72"
print(firstTry[0].rcvd_dt ?? 2999-09-09)
print(firstTry[0].cmp_dt ?? 2999-09-09)
print(firstTry[0].org_nm ?? "Bad Data")

switch firstTry[0].rcvd_dt?.compare(firstTry[0].cmp_dt ?? Date()) {
    case .orderedAscending     :   print("rcvd_dt is earlier than cmp_dt")
    case .orderedDescending    :   print("rcvd_dt is later than cmp_dt")
    case .orderedSame          :   print("The two dates are the same")
    default                    :   print("No answer")


Powered by WPeMatico

You may also like...

Comments are closed.