RealmSwift: Convert Results to Swift Array
I found a solution. Created extension on Results.
extension Results {
func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for i in 0 ..< count {
if let result = self[i] as? T {
array.append(result)
}
}
return array
}
}
and using like
class func getSomeObject() -> [SomeObject]? {
let objects = Realm().objects(SomeObject).toArray(SomeObject) as [SomeObject]
return objects.count > 0 ? objects : nil
}
How to convert an array to List in Realm?
Note that your problem is not about converting List
to array, but rather converting an array to List
. You already know how to do the former: Array(someList)
, but since you are assigning an array (movie.genreIDs ?? []
) to a List
property (movieRealm.genreIDs
), you need to convert an array to a List
.
But wait! movieRealm.genreIDs
is a let
constant, so you can't assign to it anyway. Your real problem is that you haven't declared the genreIDs
property correctly.
If you follow the docs on how to declare a property of a List
type:
class CachedMovies: Object {
// genreIDs should not be optional
let genreIDs = List<Int>()
}
You'll see that you don't need to convert arrays to List
. You can just add the contents of the array to the List
, since the list is not nil:
movieRealm.genreIDs.append(objectsIn: move.genreIDs ?? [])
Note that if you actually want an optional list property (a property which can be nil, empty, or have elements), Realm doesn't support that.
Realm Swift - Convert an array of results into an array of Ints
You can simply use Array(realm.objects(RealmType.self))
, which will convert the Results<RealmType>
instance into an Array<RealmType>
.
However, there are other serious flaws with your code. First of all, neither of the last two lines will compile, since firstly realm.objects()
accepts a generic input argument of type Object.Type
and Data
doesn't inherit from Object
. You can't directly store Data
objects in Realm
, you can only store Data
as a property of a Realm Object
subclass.
Secondly, myArray[results]
is simply wrong, since results
is supposed to be of type Results<RealmType>
, which is a collection, so using it to index an Array
cannot work (especially whose Element
type is different).
Convert Swift Array of Realm Objects Back to Realm Results
Do this:
let entryObjects = realm.objects(Entry.self).filter(...)
let total = entryObjects.map({ $0.fieldValues.sum(ofProperty: "total") }).reduce(0, +)
When Realm support aggregate operations on nested keypaths (#3392) it'll be possible to use KVC for this, but that's still in progress.
how to convert RealmSwift List to Results?
Both List
and Results
(as well as LinkingObjects
) can be converted into an AnyRealmCollection
type. I think this is probably the best way to standardize all of Realm's array-type types:
var dataSource:AnyRealmCollection!
let aRealmObject = realm.objectForPrimaryKey(SomeObject.self, key: objectId)
dataSource = AnyRealmCollection(aRealmObject.someList)
Convert Array of [[Double]] to String (similar to Print() output)
You can try
let intervals = [[1,1],[2,2],[3,3]]
let res = "[\(intervals.map { "[\($0.first!),\($0.last!)]" }.joined(separator: ","))]"
print(res)
OR
let res = String(data: try! JSONEncoder().encode(intervals), encoding: .utf8)!
OR
let res = String(describing: intervals)
Related Topics
Center Content of Uiscrollview When Smaller
Multiple Uilabels Inside a Self Sizing Uitableviewcell
Swift - Check If a Timestamp Is Yesterday, Today, Tomorrow, or X Days Ago
How to Get Touches When Parent View Has Userinteractionenabled Set to No in iOS
Sharing Code Between Original iOS App and App Extension
Application Loader Stuck at "Authenticating with the Itunes Store" When Uploading an iOS App
iOS 10 Error [Access] <Private> When Using Uiimagepickercontroller
Class Has No Initializers Swift
Dealing with iPad Mini Screen Size
How to Detect Delete Key on an Uitextfield in iOS 8
iPhone "Slide to Unlock" Animation
How to Open Mail App from Swift
iOS UI Testing on an Isolated View
When Should I Use the Various Storage Mechanisms in iOS
Exc_Bad_Access When Building Nspredicate
Access Container View Controller from Parent iOS