我使用UICollectionView
来显示各种图像。我将图像的路径存储在领域中。我想创建一个图像数组并将其存储在所有图像中的所有图像中。
我的代码:
我的领域对象
class cityDetail: Object {
dynamic var id = UUID().uuidString
dynamic var cityName = ""
dynamic var descrp = ""
dynamic var state = ""
dynamic var season = ""
dynamic var longitude = ""
dynamic var latitude = ""
var imgList = List<cityImage>()
override static func primaryKey() -> String? {
return "id"
}}
class cityImage:Object {
dynamic var imgStr = ""}
我的功能:
@IBAction func setIMG(_ sender:UIButton) {
self.img.isHidden = true
self.imgArray.append(imgURL)
print(imgArray)
newImage.imgStr = imgURL
citydetails.imgList.append(newImage)
let realm = try! Realm()
try! realm.write {
realm.add(newImage)
}
self.collection.reloadData()
}
我想要图像路径数组的函数:
@IBAction func sendData(_ sender:UIButton) {
let citydetails = cityDetail()
let newImage = cityImage()
citydetails.cityName = self.cityName.text!
citydetails.descrp = self.descrp.text!
citydetails.state = self.state.text!
citydetails.season = self.season.text!
citydetails.latitude = String(lon)
citydetails.longitude = String(lat)
citydetails.imgList = newImage
let realm = try! Realm()
do {
try realm.write() {
realm.add(citydetails)
}
} catch {
}
self.collection.reloadData()
self.img.isHidden = true
}