swiftcloudkit

How do I remove "Optional()" from object in an array


So im using CloudKit and fetching all the data into an array as [StartDay], my StartDay class looks like this:

import UIKit
import CloudKit

class StartDay {

var recordID: CKRecord.ID!
var wakeUp: String!
var sleptWell: String!
var dNN: String!
var created: String! {

    get { 
        return created
    }
}
}`

My function loads get an arraylist, which contains information received from the database. In my database it stands like this: "22.01.09:

    func checkIfButtonShouldBeEnabled(startDayList: [StartDay]){

    let startDayDates = startDayList.map{$0.created}

    for i in 0..<startDayDates.count {

        print(startDayDates)

    }

}`

OUTPUT: Optional("22.01.2019") Optional("22.01.2019")

I want to remove "Optional()", so it only says "22.01.2019", how can I do so?

UPDATE: FETCH FUNC

func loadStartDay() -> [StartDay]{

    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "StartDay", predicate: predicate)
    let operation = CKQueryOperation(query: query)

    var startDays: [StartDay] = []
    operation.desiredKeys = ["wakeUp", "wellSlept", "dNN", "recordID", "createdDato"]

    operation.recordFetchedBlock = { (record:CKRecord) in
        let newStartDay = StartDay()

        newStartDay.wakeUp = record.object(forKey: "wakeUP") as? String
        newStartDay.sleptWell = record.object(forKey: "sleptWell") as? String
        newStartDay.dNN = record.object(forKey: "dNN") as? String
        newStartDay.recordID = record.object(forKey: "recordID") as? CKRecord.ID
        newStartDay.created = record.object(forKey: "createdDato") as? String

        print(newStartDay.created)

        startDays.append(newStartDay)

    }

Solution

  • As you designed the database model you exactly know which record attributes always exist. Declaring class properties as implicit unwrapped optional as an alibi not to write an initializer is very bad practice.

    Assuming every attribute in a record does have a value declare the properties as non-optional and write an initializer.

    At least created and recordID are supposed to have always a value!

    import UIKit
    import CloudKit
    
    class StartDay {
    
        var recordID: CKRecord.ID
        var wakeUp: String
        var sleptWell: String
        var dNN: String
        var created: String 
    
        init(record : CKRecord) {
            // recordID can be retrieved directly
            self.recordID = record.recordID
            self.wakeUp = record.object(forKey: "wakeUP") as! String
            self.sleptWell = record.object(forKey: "sleptWell") as! String
            self.dNN = record.object(forKey: "dNN") as! String
            self.created = record.object(forKey: "createdDato") as! String
        }
    }
    

    and create instances with

    operation.recordFetchedBlock = { record in
        startDays.append(StartDay(record: record))
    }
    

    Now the Optional has gone.

    print(startDayList.map{ $0.created })