简体   繁体   中英

Save array of images with Realm in swift

okay, i have an app with 3 view controllers and in 2 of them i have 5 arrays of images. what i want to do is save each array of images using realm. the arrays are mutable and the user adds the images to the array of their choosing in vc1 and can send them to the arrays in vc2, but im not sure if i can just replace (in vc1) this:

var array: [UIImage] = [] {

    didSet{

        cView1.reloadData()


    }
}

with This:

dynamic var array: [UIImage] = [] {

    didSet{

        cView1.reloadData()


    }
}

i am also getting this error when i try to inherit RLMObject. "multiple inheritance from classes uiviewcontroller and RLMObject" here is my code

 class CViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, CDelegate , RLMObject 

im fairly new to ios developing so any little bit helps thanks in advance

The best approach is to save file path in DB, not image itself. So you need to create array of image paths instead array of images. Like this: let images = List<String>()

Also according to Realm Docs 'List' can't be dynamic:

When added as a property on Object models, the property must be declared as let and cannot be dynamic.

And final, you must to inherit from 'Object', not 'RLMObject' and Realm class must be separate, like in this official example:

class Dog: Object {
    dynamic var name: String = ""
    dynamic var adopted: Bool = false
    let siblings = List<Dog>()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM