0

弹出的代码没有错误,当应用程序在模拟器中加载时,它是完全空白的。我认为这可能是 PList 数据未正确填充到数组中的问题,但我不确定,因为即使页眉/页脚也没有显示。

这是其中一个 Plist 的示例(总共有 4 个 plist,都具有相同的结构,只是分配给不同的对象):

以下是分配给数组的对象之一的代码,然后将这些数组的各个部分放入单独的数组中,以跟踪它们的缩略图和标签名称:

//BEGIN INGREDIENT ITEM DATA
    NSString *ingredientItemsPath = [[NSBundle mainBundle] pathForResource:@"IngredientList" ofType:@"plist"];
    NSMutableArray *ingredientItemsArray = [[NSMutableArray alloc] initWithContentsOfFile:ingredientItemsPath];
    /*NSString *ingredientItemsPath = [[NSBundle mainBundle] pathForResource:@"ingredientList" ofType:@"plist"];
    NSDictionary *ingredientItemsDict = [[NSDictionary alloc] initWithContentsOfFile:ingredientItemsPath];
    NSArray *ingredientItemsArray = [NSArray arrayWithArray:[ingredientItemsDict objectForKey:@"Root"]];*/
    NSMutableArray *cCalcIngredientObjects = [[NSMutableArray alloc] init];
    for (int index=0; index < ingredientItemsArray.count; index++)
    {
        CCalcIngredients* ingredient = [[CCalcIngredients alloc] init]; // This is your object
        NSDictionary* ingredientDict = (NSDictionary*)ingredientItemsArray[index];
        ingredient.name = ingredientDict[@"name"];
        ingredient.thumbnail = ingredientDict[@"thumbnail"];
        NSDictionary* ingredientNutritionDict = (NSDictionary*)ingredientDict[@"nutrition"];
        ingredient.calories = ingredientNutritionDict[@"calories"];
        ingredient.totalFat = ingredientNutritionDict[@"totalFat"];
        ingredient.saturatedFat = ingredientNutritionDict[@"saturatedFat"];
        ingredient.transFat = ingredientNutritionDict[@"transFat"];
        ingredient.cholesterol = ingredientNutritionDict[@"cholesterol"];
        ingredient.sodium = ingredientNutritionDict[@"sodium"];
        ingredient.totalCarbs = ingredientNutritionDict[@"totalCarbs"];
        ingredient.dietaryFiber = ingredientNutritionDict[@"dietaryFiber"];
        ingredient.proteins = ingredientNutritionDict[@"proteins"];
        [cCalcIngredientObjects addObject:ingredient];
    }
    //END INGREDIENT ITEM DATA

    NSLog(@"The ingredient items are %@", cCalcIngredientObjects);

    //array of photos
    self.photos = [[NSMutableArray alloc] init];
    for(CCalcMeals* item in cCalcMenuObjects){
        [self.photos addObject:item.thumbnail];
    }
    for(CCalcMeat* meat in cCalcMeatObjects){
        [self.photos addObject:meat.thumbnail];
    }
    for(CCalcSalsa* salsa in cCalcSalsaObjects){
        [self.photos addObject:salsa.thumbnail];
    }
    for(CCalcIngredients* ingredient in cCalcIngredientObjects){
        [self.photos addObject:ingredient.thumbnail];
    }
    NSLog(@"photos %@", self.photos);

当我针对我创建的所有数组运行它时,NSLog 会打印出来:

我试图弄清楚为什么这只是一个空白的黑色模拟器屏幕,并弄清楚我的 Plist 数据发生了什么以及它是否正确构建。

4

1 回答 1

0

在弄乱代码后,我发现我试图从 Plist 中提取信息作为一个数组,即使根信息实际上是一个字典,所以现在我确切地知道为什么我拥有的 NSLog 只返回 Nil。感谢您的帮助和提示!

于 2014-01-31T19:12:33.233 回答