Swift 3 中的 JSON 图像和核心数据 [关闭]

Posted

技术标签:

【中文标题】Swift 3 中的 JSON 图像和核心数据 [关闭]【英文标题】:JSON image and Core Data in Swift 3 [closed] 【发布时间】:2017-07-18 19:05:50 【问题描述】:

如何在 Core Data、Swift 3 中保存 JSON 图像?

我有a url - 这是一个GET 请求。我想保存profimgUID

如何在 Swift 3 中保存和获取它们?

【问题讨论】:

【参考方案1】:

ios 11 和 Xcode 9 开始,Core Data 支持 URI 数据类型,该数据类型适用于存储图像 URL,例如 JSON 中的 profimg 字段。您也可以使用 Core Data 的字符串类型,无论是为了方便还是如果您支持 iOS 10。UID 字段是一个整数,因此请选择 Core Data 的整数类型中的任何一个大到足以容纳最大值。如果您对最大 UID 有疑问,您可以放心地使用 Core Data 的“Integer 64”类型。

像任何其他核心数据属性一样保存和获取。

【讨论】:

嗨汤姆,请看第二个答案,我已经完成了这样的问题。但问题是当我获取数据并在表格视图中显示时,如果没有互联网连接,应用程序崩溃 这与这个问题完全不同。您应该发布一个新问题,其中包含有关您如何使用网络连接以及应用崩溃时究竟发生了什么(错误消息等)的信息。【参考方案2】:

func clearData()

    let delegate = UIApplication.shared.delegate as? AppDelegate

    if let context = delegate?.persistentContainer.viewContext


        do 




            let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Mydata")

            let objects = try(context.fetch(fetchRequest)) as? [NSManagedObject]




            for object in objects! 
                context.delete(object)

            

            try(context.save())


         catch let err 
            print(err)
        
    





func setupData() 

      clearData()

    let urlWithParams = "https://www.zdoof.com/api/zTimeline/ztimeline_bind?Created_by=4490&Created_on=01/jan/2017&Updated_on=14/june/2017&PageIndex=1&PageSize=50"

    Alamofire.request(urlWithParams, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON  response in

        switch(response.result) 
        case .success(_):
            if let data = response.result.value as? NSArray
                self.myHelp = [myHelperClass]()





                for allData in data  

                    let help = myHelperClass()
                   let jsonID = (allData as! NSDictionary).value(forKey: "UID") as! NSInteger
                    help.myId = String.init(describing: jsonID)

                    help.myName = ((allData as! NSDictionary).value(forKey: "Name") as! NSString) as String

                    self.myNameArray.append(help.myName)

                    //                            person.setValue(myName, forKey: "name")


                    DispatchQueue.main.async(execute:  () -> Void in


                        let myUrlString = "https://www.zdoof.com//"

                        let myprofileImage = (allData as! NSDictionary).value(forKey: "profimg") as! NSString
                        help.myStringImageArray = myUrlString  + (myprofileImage as String)




                       let midImage = ((allData as! NSDictionary).value(forKey: "mid_img") as! NSString) as String

                        help.myStringPostImageArray = myUrlString + (midImage as String)




                    )





                    help.Yumms = ((allData as AnyObject).value(forKey: "Postlike") as! NSString) as String


                    let kUserDefault = UserDefaults.standard



                    //                            person.setValue(Yumms, forKeyPath: "postlike")

                    let myId = (allData as AnyObject).value(forKey: "UID") as!NSInteger
                    help.myidString = String.init(describing: myId)
                    //                            person.setValue(myidString, forKey: "uID")


                    help.Rebcol = ((allData as AnyObject).value(forKey: "Rebcol") as! NSString) as String
                    //                            person.setValue(Rebcol, forKey: "rebcol")


                    help.UTYPIMG = ((allData as AnyObject).value(forKey: "UTYPIMG") as! NSString) as String
                    //                            person.setValue(UTYPIMG, forKey: "uTYPIMG")

                    help.Created_on = ((allData as AnyObject).value(forKey: "Created_on") as! NSString) as String
                    //                            person.setValue(Created_on, forKey: "created_on")

                    help.UTYP = ((allData as AnyObject).value(forKey: "UTYP") as! NSString) as String
                    //                            person.setValue(UTYP, forKey: "uTYP")

                    help.comments = ((allData as AnyObject).value(forKey: "Postcomment") as! NSString) as String

                    //                            person.setValue(comments, forKeyPath: "postcomment")

                    help.Details = ((allData as AnyObject).value(forKey: "Details") as! NSString) as String


                    //                            person.setValue(Details, forKeyPath: "details")
                    help.myPost = ((allData as AnyObject).value(forKey: "Share") as! NSString) as String


                    //                            person.setValue(myPost, forKeyPath: "share")

                    help.myDate = ((allData as AnyObject).value(forKey: "zDate") as! NSString) as String


                    //                            person.setValue(myDate, forKeyPath: "zDate")


                    let myPTYPEInteger = (allData as AnyObject).value(forKey: "PTYPE") as!NSInteger

                   help.myPTYPE = String.init(describing: myPTYPEInteger)
                   let myIdInteger  = (allData as AnyObject).value(forKey: "ID") as!NSInteger
                    help.myId = String.init(describing: myIdInteger)


                    self.myFunction(nameFunc: help.myName, yummFunc: help.Yumms, myIdFunc: help.myidString, RebcolFunc: help.Rebcol, UTYPIMGFunc: help.UTYPIMG, Created_onFunc: help.Created_on, UTYPFunc: help.UTYP, commentsFunc: help.comments, DetailsFunc: help.Details, myPostFunc: help.myPost, myDate: help.myDate ,imageArray: help.myStringImageArray , postImageArray: help.myStringPostImageArray , myUId: help.myId , myPpTypeFunc: help.myPTYPE , myIId: help.myId )



                    kUserDefault.set(self.ID, forKey: "ID")
                    kUserDefault.set(self.PTYPE, forKey: "PTYPE")
                    kUserDefault.set(self.UID, forKey: "UID")
                    kUserDefault.synchronize()


                




            
            DispatchQueue.main.async(execute:  () -> Void in

                self.myTable.reloadData()
            )
            break

        case .failure(_):
            print(response.result.error!)
            break

        


    









func myFunction(nameFunc: String , yummFunc : String , myIdFunc : String , RebcolFunc: String , UTYPIMGFunc : String , Created_onFunc : String, UTYPFunc: String , commentsFunc : String , DetailsFunc : String ,myPostFunc : String , myDate : String ,imageArray : String , postImageArray : String , myUId : String , myPpTypeFunc : String , myIId : String )




    let delegate = UIApplication.shared.delegate as? AppDelegate

    if let context = delegate?.persistentContainer.viewContext


        let person = NSEntityDescription.insertNewObject(forEntityName: "Mydata", into: context) as! Mydata


        person.setValue(nameFunc, forKey: "name")

        person.setValue(yummFunc, forKeyPath: "postlike")
        person.setValue(myIdFunc, forKey: "uID")
        person.setValue(RebcolFunc, forKey: "rebcol")
        person.setValue(UTYPFunc, forKey: "uTYPIMG")
        person.setValue(Created_onFunc, forKey: "created_on")
        person.setValue(UTYPFunc, forKey: "uTYP")
        person.setValue(commentsFunc, forKeyPath: "postcomment")
        person.setValue(DetailsFunc, forKeyPath: "details")
        person.setValue(myPostFunc, forKeyPath: "share")
        person.setValue(myDate, forKeyPath: "zDate")
        person.setValue(imageArray, forKeyPath: "profimg")
        person.setValue(postImageArray, forKeyPath: "mid_img")
        person.setValue(myUId, forKey: "iD")
        person.setValue(myPpTypeFunc, forKey: "pTYPE")


        do 

            try(context.save())
         catch let err 
            print(err)
        


       loadData()

    




函数加载数据() 让委托 = UIApplication.shared.delegate 为?应用代理

if let context = delegate?.persistentContainer.viewContext 

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Mydata")



    do 



      people = try(context.fetch(fetchRequest)) as? [Mydata]





     catch let err 
        print(err)
    


我已经这样做了,但问题是当有互联网连接时,它正在工作。没有互联网应用程序崩溃

【讨论】:

以上是关于Swift 3 中的 JSON 图像和核心数据 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

单个条目中的多个图像 - Swift 中的核心数据

swift核心数据:保存相机中的图像

JSON图像解析IOS Swift 3.0

Swift 3中Alamofire的核心数据

在 Swift 中将 JSON 字典导入核心数据

Swift 中带有自定义 TableViewCell 的核心数据图像