ios - 如何将颜色保存到 Realm 并在其他 View Controller 中显示颜色?

标签 ios swift realm uicolor

我需要在其他viewController中存储颜色和显示颜色。当我保存它时,像这样保存 UIDynamicSystemColor: 0x600001127620;名称=系统蓝色

有了这些数据,我如何显示颜色,它是 Realm 对象中的字符串类型。

我怎样才能做到这一点?

类别形式:-

import RealmSwift

class CategoryForm : Object {


    @objc dynamic var categoryName : String = ""
    @objc dynamic var categoryColor : String = ""

}

类别保存颜色:-

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)")

                                       }
                                   }

保存后这里显示保存的颜色

显示 View Controller :-

 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 
    }

最佳答案

如果您不关心暗模式和系统颜色,我会添加几个简单的扩展来将 UIColor 序列化/反序列化为标准值,例如 json 中的 ARGB 或更好的十六进制作为 Int。

周围有很多解决方案,例如:https://stackoverflow.com/a/19072934/691977

然后只存储单个 String 或 Int 值。 读取时有一个访问器方法将存储的值转换为 UIInt。

类似于:

extension UIColor {
   init(hex: UInt32){
       ...
   }
   var hex: UInt32 {
       ...
   }
}


class CategoryForm : Object {


    @objc dynamic var categoryName : String = ""
    @objc dynamic var categoryColor : UInt32 = 0

    var color: UIColor? {
        get {
            return UIColor(hex: self.categoryColor)
        }
        set {
            self.categoryColor = newValue.hex
        }
    }



}

关于ios - 如何将颜色保存到 Realm 并在其他 View Controller 中显示颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58606517/

相关文章:

android - 将 Kotlin ByteArray 保存到 Realm 中

ios - 如何在 iOS 项目中正确配置 Realm DB 文件 (<db_name>.realm) 的路径?

android - 当 Realm 文件更改时, Realm 不会在运行时更新

ios - tableView.reloadSections(sectionIndex,: . 无)无法正常工作

iOS 应用间通信

ios - Swift 3 中没有这样的模块 'GoogleMobileAds'

objective-c - 将 Swift 导入 Objective-C : Objective-C header "-Swift.h" is not created

swift - 从 Meteor JSON/Array/Cursor 创建字典数组

ios - 如何在swift项目中使用objective-c类别cocoapods

ios - SwiftUI - 具有透明背景的按钮