swift3解析模型问题

Posted

技术标签:

【中文标题】swift3解析模型问题【英文标题】:swift3 parsing with model issue 【发布时间】:2016-12-26 00:48:04 【问题描述】:

您好,我正在尝试解析这个 json 文件:http://pastebin.com/TCdkJnvZ

这是我用我要解析的信息制作的类:

public class Recipe: NSObject 

  var recipeID : NSNumber?
  var categoryName : String?
  var ingredients : [Int : Ingredients]?
  var nutrition : [Nutrition]?
  var imageName : String?
  var instructions : [Int : String]?



class Ingredients : NSObject 

   var id : NSNumber?
   var name : String?
   var quantity: NSNumber?
   var unit : String?



class  Nutrition : NSObject 

  var serving : String?
  var calories : NSNumber?
  var fat : String?
  var carbs : NSNumber?

 

这张图片是当前的问题..我真的不确定我在这里做错了什么..所以如果我能在修复我的逻辑/问题上获得任何帮助,我们将不胜感激。

 func parseToJSON(data: Any) 

    // add meals to here
    var recipes : [Recipe]
    // single meals here
    var meals : Recipe

    do 

        if let json = try JSONSerialization.jsonObject(with: data as! Data) as? [String: Any],
        meals.recipeID == json["recipeID"] as! NSNumber? ,
        meals.imageName == json["ImageURL"] as! String?,
        //meals.instructions == meals.parseInstructions(instructions: (json["Instructions"] as! String)),
        meals.categoryName == "Meals" ,

        let ingredients = json["Ingredients"] as! [[String: Any]]? 

                for items in ingredients 
                    var i : Int = 0
                    var groceryItems : Ingredients

                    groceryItems.id = items["IngredientID"] as? NSNumber
                    groceryItems.name = items["Name"] as? String
                    groceryItems.quantity = items["Quantity"] as? NSNumber
                    groceryItems.unit = items["Unit"] as? String

                    meals.ingredients?[i] = groceryItems
                
            ;

        let nutritionInfo = json["NutritionInfo"] as! [[String: Any]]? 

                for items in nutritionInfo 

                    var nutrition : Nutrition

                    nutrition.serving = items["SingularYieldUnit"] as? String
                    nutrition.calories = items["TotalCalories"] as? NSNumber
                    nutrition.fat = items["TotalFat"] as? String
                    nutrition.carbs = items["TotalCarbs"] as NSNumber

                    meals.nutrition = nutrition
                
        ;
    
    catch

    


【问题讨论】:

请发布实际代码,不是代码的图像 好的,我添加了代码 【参考方案1】:

您似乎有多种语法错误,但编译器一次只能显示一个问题。我已经为您稍微清理了代码,这应该会将您推向正确的方向。我无法完全解决它,因为我不知道你的确切意图是什么。

这里是更新后的parseToJSON函数:

func parseToJSON(data: Any) 

    let meals = Recipe()

    do 

        if let json = try JSONSerialization.jsonObject(with: data as! Data) as? [String: Any] 

            meals.recipeID == json["recipeID"] as! NSNumber?
            meals.imageName == json["ImageURL"] as! String?
            //meals.instructions == meals.parseInstructions(instructions: (json["Instructions"] as! String)),
            meals.categoryName == "Meals"

            if let ingredients = json["Ingredients"] as! [[String: Any]]? 
                for items in ingredients 
                    let groceryItems = Ingredients()
                    groceryItems.id = items["IngredientID"] as? NSNumber
                    groceryItems.name = items["Name"] as? String
                    groceryItems.quantity = items["Quantity"] as? NSNumber
                    groceryItems.unit = items["Unit"] as? String
                    meals.ingredients?.append(groceryItems)
                
            

            if let nutritionInfo = json["NutritionInfo"] as! [[String: Any]]? 
                for items in nutritionInfo 
                    let nutrition = Nutrition()
                    nutrition.serving = items["SingularYieldUnit"] as? String
                    nutrition.calories = items["TotalCalories"] as? NSNumber
                    nutrition.fat = items["TotalFat"] as? String
                    nutrition.carbs = items["TotalCarbs"] as? NSNumber
                    meals.nutrition?.append(nutrition)
                
            

        

    
    catch

    


我还将Recipe 对象的ingredients 属性更改为:

var ingredients : [Ingredients]?

主要问题是您的很多代码都在 if let 表达式中,并且您的缩进已关闭,因此您无法轻易分辨。

【讨论】:

以上是关于swift3解析模型问题的主要内容,如果未能解决你的问题,请参考以下文章

swift3 模型转字典(JSON)

swift3 模型转字典(JSON)

Swift 3 - SceneKit 获取 3d 模型比例值

Swift 3 核心数据 NSInternalInconsistencyException entityForName 无法在此模型中找到名为 Foo 的实体

从核心数据模型 Swift 3 中删除一行

SceneKit 与 Vuforia AR 库自定义模型