Computed properties

Can you say a little more about computed properties. I like to include them in my model as it helps keep the code clean. I’m interested in learning more about them. Are they resource intensive, when are they computed. That kind of thing.

Great book by the way.



Hi Rob, computed properties aren’t a Realm specific feature per-se, rather a standard Swift feature. The computations are executed upon access, there’s a few paragraphs about computed and read-only computed properties in the Swift docs: (scroll a bit down to find the relevant sections)

I personally like using them in Realm models, especially when it comes to wrapping types that Realm doesn’t support directly :+1:t3:

Swift Apprentice: Chapter 11 “Properties” covers:

  • Stored Properties and Computed Properties,
  • Type properties
  • Property observers
  • Lazy properties

iOS Apprentice: Chapter 21 “Swift Review” touches on a little bit of everything in Swift, and Computed properties specifically on page 494.

Some key points of Computed Properties:

  • Computed on the fly - return value is computed only when requested in your code. (similar to how you utilize the lazy keyword for stored properties)
  • Read-only if it only has a getter: get { }
  • Read-write if has both a getter & a setter: get {} set {}
  • Setter parameter is newValue but you can use any constant name you’d like by replacing newValue with your constant in:
set (newValue) {
    varToBeSet = newValue

Marin, computed properties and List? How do I “write” a CLLocation to a List?

@objcMembers class PolyLineDataModel: Object {
dynamic var key = UUID().uuidString
dynamic var title = “”
var positions = List< PositionDataModel>()

override static func primaryKey() -> String? {
    return "key"



@objcMembers class PositionDataModel: Object {

dynamic var key = UUID().uuidString

private let lat = RealmOptional<Double>()
private let lng = RealmOptional<Double>()

var lastLocation: CLLocation? {
    get {
        guard let lat = lat.value, let lng = lng.value else {
            return nil
        return CLLocation(latitude: lat, longitude: lng)
    set {
        guard let location = newValue?.coordinate else {
            lat.value = nil
            lng.value = nil
        lat.value = location.latitude
        lng.value = location.longitude


Thank you for a great Realm book and realm videos!!!


1 Like

Hey Tom, thanks :slight_smile: What is the specific issue you’re having? PositionDataModel is a “normal” realm object so List should be able to handle it as usual. Do you get any errors?

Yes Marin:

Screen Shot 2018-06-01 at 22.16.17

Any suggestions?


You cannot assign a CLLocation object to the expected type List. What do you want to achieve?

I want to store an unspecified number of Position to an object (polyline, area, marker(only one position)). I intend to have a class for each object and a class for the positions.

I was able to resolve the problem Marin:

Screen Shot 2018-06-02 at 12.41.55

Thank you for turning me in the right direction: “PositionDataModel is a “normal” realm object …”!