swift3

2016-12-26 1 views
0

나는이 JSON 파일을 구문 분석하려고 안녕하세요 :swift3

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? 

} 

이 이미지는 다음과 같습니다 여기 http://pastebin.com/TCdkJnvZ

내가 구문 분석 할 내가 정보 만든 클래스입니다 현재 문제가 .. 나는 정말로 내가 뭘 잘못하고 있는지 잘 모르겠다. 그래서 만약 내가 내 논리/문제를 해결에 대한 도움을받을 수 있다면 그것을 인정할 것이다.

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{ 

    } 

} 
+0

코드의 이미지 *가 아닌 실제 코드를 게시하십시오. –

+0

확인 코드를 추가했습니다. – codeNinjaBro

답변

1

다양한 구문 오류가있는 것 같지만 컴파일러는 한 번에 하나의 문제 만 표시 할 수 있습니다. 코드를 약간 정리 했으므로 올바른 방향으로 밀어 넣어야합니다. 나는 당신의 정확한 의도가 무엇인지 모르기 때문에 그것을 완전히 고칠 수는 없습니다. 여러분의 많은 코드가 if let을 내부에 주요 문제

var ingredients : [Ingredients]? 

했다되었다 :

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 속성을 변경 : 여기

업데이트 된 parseToJSON 기능입니다 표현과 당신의 들여 쓰기가 꺼져서 쉽게 말할 수 없었습니다.