不兼容的指针类型分配给' NSArray'到#NSString'

时间:2015-02-14 06:59:25

标签: ios objective-c iphone ios7 nsstring

RecipeBookViewController.m文件在Xcode中显示警告“不兼容的指针类型分配给'NSArray'到'NSString'

任何人都可以修复此警告。

我正在从appcoda网站教程#14

开发RecipeBook应用程序

这是我的Recipe.h文件

#import <Foundation/Foundation.h>

@interface Recipe : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *prepTime;
@property (strong, nonatomic) NSString *imageFile;
@property (strong, nonatomic) NSString *ingredients;

@end

这是我的Recipe.m文件

#import "Recipe.h"

@implementation Recipe
@synthesize name,prepTime,imageFile,ingredients;

@end

这是我的RecipeBookViewController.m文件

#import "RecipeBookViewController.h"
#import "RecipeDetailViewController.h"
#import "Recipe.h"

@interface RecipeBookViewController ()

@end

@implementation RecipeBookViewController 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data
    //recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

    Recipe *recipe1 = [Recipe new];
    recipe1.name = @"Egg Benedict";
    recipe1.prepTime = @"30 min";
    recipe1.imageFile = @"egg_benedict.jpg";
    recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs", @"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil];
}

3 个答案:

答案 0 :(得分:3)

问题是成分是NSString,即

@property (strong, nonatomic) NSString *ingredients;

但是,正如错误所述,您正在尝试将数组分配给NSString,即

recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs",...

因此,您可以将成分更改为NSArray以更正此错误,例如:

@property (strong, nonatomic) NSArray *ingredients;

答案 1 :(得分:1)

成分属于类nsstring ....将其更改为Nsarray。

@property (strong, nonatomic) NSArray *ingredients;

答案 2 :(得分:0)

您的recipe1.ingredientsNSString类型,这就是您收到此警告的原因所以请将其更改为NSArray类型。

@property (strong, nonatomic) NSArray *ingredients;