I need to store color and display color in other viewController. When i'm saving it saving like this UIDynamicSystemColor: 0x600001127620; name = systemBlueColor
With that data how can i display color and it was in string type in realm object.
How can i do that?
CategoryForm :-
import RealmSwift
class CategoryForm : Object {
@objc dynamic var categoryName : String = ""
@objc dynamic var categoryColor : String = ""
}
category SaveColor :-
struct CategoryVaraible {
static var categoryArray : Results<CategoryForm>?
}
class CategoryDetailsViewController : UIViewController {
var categories = CategoryVaraible.categoryArray
var color = [UIColor.secondarySystemFill,UIColor.systemRed,UIColor.systemPink,UIColor.systemOrange,UIColor.systemYellow,UIColor.systemGreen,UIColor.systemBlue,UIColor.systemPurple]
var colorSelected : UIColor!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "ColorViewCell", for: indexPath) as! ColorViewCell
cell.backgroundColor = color[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = indexPath.row
if item == 0 {
colorSelected = UIColor.secondarySystemFill
}else if item == 1 {
colorSelected = UIColor.systemRed
}else if item == 2 {
colorSelected = UIColor.systemPink
}else if item == 3 {
colorSelected = UIColor.systemOrange
}else if item == 4 {
colorSelected = UIColor.systemYellow
}else if item == 5 {
colorSelected = UIColor.systemGreen
}else if item == 6 {
colorSelected = UIColor.systemBlue
}else if item == 7 {
colorSelected = UIColor.systemPurple
}
}
//Here Saving
@IBAction func saveAction(_ sender: UIButton) {
let newArray = CategoryForm()
newArray.categoryName = categoryName.text!
newArray.categoryColor = "\(colorSelected!)"
self.saveItems(category: newArray)
self.navigationController?.popViewController(animated: true)
}
func saveItems(category: CategoryForm) {
do{
try realm.write {//Save/Create
realm.add(category)
}
}catch {
print("Error in saving \(error)")
}
}
After saving Here to display the saved color
Displaying ViewController :-
var categories = CategoryVaraible.categoryArray
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories?.count ?? 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) as! CategoryCell
let colorStr = categories?[indexPath.row].categoryColor
<UIDynamicSystemColor: 0x600001127620; name = systemBlueColor>
cell.circleImage.backgroundColor = UIImage(name: colorStr as! String)//Here We need to display color
return cell
}