分配给 'NSArray' 到 'NSString' 的不兼容指针类型
Incompatible pointer types assigning to 'NSArray' to 'NSString'
RecipeBookViewController.m 文件在 Xcode 中显示警告“分配给 'NSArray' 到 'NSString'
的不兼容指针类型
任何人都可以修复此警告。
我正在从教程 Tutorial #14 的 appcoda 网站开发 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];
}
您的 recipe1.ingredients
是 NSString
类型,这就是您收到此警告的原因,因此请将其更改为 NSArray
类型。
@property (strong, nonatomic) NSArray *ingredients;
问题是配料是 NSString
,即
@property (strong, nonatomic) NSString *ingredients;
但是,如错误所述,您正在尝试将数组分配给 NSString
,即
recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs",...
因此您可以将成分更改为 NSArray
以更正此错误,例如:
@property (strong, nonatomic) NSArray *ingredients;
成分是 class nsstring....将其更改为 Nsarry。
@property (strong, nonatomic) NSArray *ingredients;
RecipeBookViewController.m 文件在 Xcode 中显示警告“分配给 'NSArray' 到 'NSString'
的不兼容指针类型任何人都可以修复此警告。
我正在从教程 Tutorial #14 的 appcoda 网站开发 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];
}
您的 recipe1.ingredients
是 NSString
类型,这就是您收到此警告的原因,因此请将其更改为 NSArray
类型。
@property (strong, nonatomic) NSArray *ingredients;
问题是配料是 NSString
,即
@property (strong, nonatomic) NSString *ingredients;
但是,如错误所述,您正在尝试将数组分配给 NSString
,即
recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs",...
因此您可以将成分更改为 NSArray
以更正此错误,例如:
@property (strong, nonatomic) NSArray *ingredients;
成分是 class nsstring....将其更改为 Nsarry。
@property (strong, nonatomic) NSArray *ingredients;