I am getting this error "use of undeclared identifier 'recipe'

1.1k Views Asked by At

Please refer to below code.

#import "RecipeDetail.h"
#import "Recipe.h"

@implementation RecipeDetail

@synthesize recipeLabel;
@synthesize recipeName;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = recipe.name;
    self.prepTimeLabel.text = recipe.prepTime;
    self.recipePhoto.image = [UIImage imageNamed:recipe.imageFile];

    NSMutableString *ingredientText = [NSMutableString string];
    for (NSString* ingredient in recipe.ingredients) {
        [ingredientText appendFormat:@"%@\n", ingredient];
    }
    self.ingredientTextView.text = ingredientText;

}
2

There are 2 best solutions below

0
Dharmesh Dhorajiya On

you alloc you class object in viewDidLoad method like this.

Recipe *recipe=[[Recipe alloc]init];

then after you can use recipe object like that.

self.title = recipe.name;
0
Asad Javed On

You are getting the Correct error.

The the variable recipe is undeclared and you are trying to assign values from a non-existing variable.

So, either you should declare it before using it in your viewDidLoad method like:

Recipe *recipe = [Recipe alloc] init];

and then use it.

Or if you want to pass an existing recipe variable from outside this class, as it appears that you are trying to do that, then where ever you are declaring and initializing an instance of this class RecipeDetial make a Recipe property in RecipeDetail.h like:

@property (strong, nonatomic) Recipe *recipe;

and assign that variable to this property before presenting this UIViewController and use it as you are using it already.

Hope this helps...