Created
July 29, 2015 18:59
-
-
Save jpsim/8cdc58c6d14aa247a048 to your computer and use it in GitHub Desktop.
Representing an array of strings in Realm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Although this example demonstrates how to store flat arrays of strings | |
on a Realm model, you can extend this pattern to store anything from | |
arrays of integers to native Swift enum's. Basically anything that you can | |
map to a representable type in Realm. | |
*/ | |
class RealmString : Object { | |
dynamic var stringValue = "" | |
} | |
class Person : Object { | |
var nicknames: [String] { | |
get { | |
return _backingNickNames.map { $0.stringValue } | |
} | |
set { | |
_backingNickNames = newValue.map { RealmString(value: [$0]) } | |
} | |
} | |
dynamic var _backingNickNames = List<RealmString>() | |
} |
I got it working by adding the transfer property to ignore and changing how it's filled:
class RealmString : Object {
dynamic var stringValue = ""
}
class Person : Object {
var nicknames: [String] {
get {
return _backingNickNames.map { $0.stringValue }
}
set {
_backingNickNames.removeAll()
_backingNickNames.append(contentsOf: newValue.map({RealmString(value: $0)}))
}
}
dynamic var _backingNickNames = List<RealmString>()
override static func ignoredProperties() -> [String] {
return ["nicknames"]
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any resolution for this issue? Facing same issue (map' produces '[T]', not the expected contextual result type) on updating to Swift 3.