簡體   English   中英

分配給'NSString'到'NSString'的不兼容指針類型

[英]Incompatible pointer types assigning to 'NSArray' to 'NSString'

RecipeBookViewController.m文件在Xcode中顯示警告“將'NSArray'分配給'NSString'的指針類型不兼容

任何人都可以解決此警告。

我正在從教程#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];
}

問題是配料是NSString ,即

@property (strong, nonatomic) NSString *ingredients;

但是,由於錯誤狀態,您正在嘗試將數組分配給NSString ,即

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

因此,您可以將成分更改為NSArray來糾正此錯誤,例如:

@property (strong, nonatomic) NSArray *ingredients;

成分屬於nsstring類。...將其更改為Nsarry。

@property (strong, nonatomic) NSArray *ingredients;

您的recipe1.ingredientsNSString類型,這就是為什么您收到此警告的原因,因此請將其更改為NSArray類型。

@property (strong, nonatomic) NSArray *ingredients;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM