核心数据获取结果未快速添加到自定义类中

时间:2018-09-16 07:42:49

标签: ios swift core-data

我正在从coredata提取数据,但工作正常,但是我将coredata值附加到自定义类中,当show提取数据时显示为

  name Optional(Rahul Gandhi)desc Optional(Indian politician) profileimage Optional(https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Rahul_Gandhi_%28headshot%29.jpg/37px-Rahul_Gandhi_%28headshot%29.jpg)
    name Optional(Rahul Raj)desc Optional(Indian composer) profileimage Optional(https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/Shiva_Temple%2C_Dhoni%2C_Palakkad.jpg/37px-Shiva_Temple%2C_Dhoni%2C_Palakkad.jpg)

但是将核心数据附加到自定义类之后,我在表视图中显示了该数据,但显示为空

 class ModelData
{
    var name : String?
    var Description:String?
    var ProfileImage:String?
    init(name:String?,Desctiption:String,ProfileImage:String) {
        self.name = name
        self.Description = Desctiption
        self.ProfileImage = ProfileImage
    }

var modeldata = [ModelData]()

    func corefetch()
        {
            do{
                let fetchRequest : NSFetchRequest<Val> = Val.fetchRequest()
                let Data = try context.fetch(fetchRequest)
                if Data.count > 0
                {
                    for actor in Data as [Val]
                    {
                        let nameval = actor.value(forKey: "title")
                        let profileImage = actor.value(forKey: "profile")
                        let descrption = actor.value(forKey: "desc")
                        print("name \(String(describing: nameval))desc \(String(describing: descrption)) profileimage \(String(describing: profileImage))")
                        self.modeldata.append(ModelData.init(name: nameval as? String, Desctiption: descrption as! String, ProfileImage: profileImage as! String))
                    }
                }

            }catch { print(error) }
        }

我尝试了许多方法,但没有找到如何将coredata值附加到自定义类的解决方案

1 个答案:

答案 0 :(得分:0)

别忘了保存数据

do {          
   try context.save()       
  } catch {       
   print("Failed saving")
}