We are currently using RealmSwift to store data.
Several types of data are stored in one object, as shown below.
class Sample: Object {
dynamic var number —CGFloat=0
dynamic var string: String=""
}
At this time,
var floatArr: CGFloat = [ ]
As long as there are only "number" objects in the CGFloat type array as described above.
Is that possible?
I couldn't find a site that might be helpful, so
I am trying to implement it while looking at the official website.
It's hard to implement.
For Results
, you can use valueForKey(_:)
or valueForKeypath(_:)
to extract only a specific set of properties.
let numbers=realm
.objects (Sample.self)
.valueForKey ("number")
The numbers
type is NSArray
, so you need a cast to treat it as an array of CGFloat
.
iflet numbers=number as?[CGFloat]{
...
}
Another solution is to use map()
.
let numbers=realm
.objects (Sample.self)
.map {$0.number}
If you want to add up or average what you want to do in the end, you can use the sum()
or average()
methods directly without bothering to array them.
By the way, CGFloat
is not recommended as a data type to store in Realm because it varies from device to device (32-bit or 64-bit).For example, if you change from a 32-bit device to a 64-bit device and restore the data, the data type is different, so migration is required.Use Double (or Float).
884 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
603 Uncaught (inpromise) Error on Electron: An object could not be cloned
566 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
567 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.