How do I use getters and setters for a variable of type array to access items of the array by index instead of the entire array itself

class SomeClass {
     var dates: [Date] {
          get {
               return ckRecord.object(forKey: "dates") as! [Date]
          }
          set(newDates) {
               ckRecord.setObject(newDates as! CKRecordValue, "dates")
          }
     }
}    


In the previous code, how do I write code in the get and set closures to save to CloudKit and retrieve the data from CloudKit everytime I get one of the values from the array or set one of the values in the array, which means I don't retrieve the whole array or set the whole array, only one of the values at a given index as in the following code:


var obj = SomeClass()
obj.dates[0] = Date()


I don't have a problem using CloudKit. I have a problem figuring out how to arrange the code for the get and set closures so that I properly access the array from the CloudKit record by index. I am attempting to wrap the CloudKit record in class SomeClass.


Any help will be appreciated.

Accepted Reply

Could you give me some references or key words to search for in documentation?

I don’t think there’s any documentation that explicitly covers these approaches. Rather, you have to build the approach by combining ideas that you’re probably already familiar with, namely structs, classes, and subscripts.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

There’s a couple of ways you could approach this:

  • Create your own type, call it

    DateArray
    , that supports subscripts, and use that for you
    dates
    property (A).
  • Change your approach so that you convert your

    CKRecord
    to a standard Swift model object on input and then convert it back when you go to save (B).

Which is best really depends on what sort of flexibility you want in the clients of this class. If those clients only do a limited set of operations on

dates
, option A might be reasonable. However, if you want the clients to be able to use
dates
exactly like a real Swift array, it’s likely that option A will be hard and a switch to option B would be in order.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

How would I do either of those. Could you give me some references or key words to search for in documentation?

Could you give me some references or key words to search for in documentation?

I don’t think there’s any documentation that explicitly covers these approaches. Rather, you have to build the approach by combining ideas that you’re probably already familiar with, namely structs, classes, and subscripts.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"