0

我有 NSMutableArray 如下:

在.h

@property (nonatomic,retain) NSMutableArray *arrReceipes; 

在.m

@synthesize arrReceipe;   

在方法内部

arrReceipes = [[NSMutableArray alloc] init];    
NSArray *arrReceipesTime1;    
NSArray *arrReciepHeart;     
NSArray *arrRecipeLifestyle;     

arrReceipesTime1 = [NSArray arrayWithObjects:@"Half an hour or less",@"About an     hour",@"More than an hour", nil];     

NSDictionary *arrReceipeTime1Dict = [NSDictionary dictionaryWithObject:arrReceipesTime1 forKey:@"Find Recipes"];    

arrReciepHeart = [NSArray arrayWithObjects:@"HealthyAndDelicius", nil];    
NSDictionary *arrRecipesHeartDict = [NSDictionary dictionaryWithObject:arrReciepHeart   forKey:@"Find Recipes"];     

arrRecipeLifestyle = [NSArray arrayWithObjects:@"Recipe for fall",@"Quick and   easy",@"Cooking for kids",@"Easy entertaining",@"American classics",@"Outdoor cooking",   nil];     
NSDictionary *arrRecipeLifestyleDict = [NSDictionary dictionaryWithObject:arrRecipeLifestyle forKey:@"Find Recipes"];     

[arrReceipes addObject:arrReceipeTime1Dict];
[arrReceipes addObject:arrRecipesHeartDict];
[arrReceipes addObject:arrRecipeLifestyleDict];

那就是“arrReceipes”是我的 NSMutable 数组。

现在我想提取“arrReceipes”的值/字符串并将其放入“NSArray * somevariable”中。

我怎样才能做到这一点?

我这样做是为了:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
       NSIndexPath *indexPath = [self.tableView1 indexPathForSelectedRow];
       RecipeDetailViewController *destViewController = segue.destinationViewController;

       destViewController.recipeName = [arrReceipes objectAtIndex:indexPath.row];
    }
}

在我上面的第 3 行中(在“if”中),如果我在“main”方法中给出“arrReceipes”它的抛出异常。但是,如果不是给出“arrReceipes”,我会给“somevariable”它的工作正常。

希望你能理解我的问题。请帮忙。谢谢。

4

3 回答 3

1

您要放入的对象arrReceipes是字典,因此destViewController.recipeName如果您想将其中一个对象传递给它,则应该是字典。

于 2012-12-05T15:00:59.013 回答
0

你可以试试

NSArray *somevariable = [NSArray arrayWithArray:arrReceipes];

NSMutableArray 是 NSArray 的子类,因此您可以只使用 NSArray 方法

于 2012-12-05T18:11:36.473 回答
0

你不能在 NSArray 中写入。

但是,您可以执行以下操作:

NSArray * somevariable = [arrReceipes copy];

您还可以修改代码以将数据写入 NSMutableArray,然后将其复制到arrRecipes

于 2012-12-05T15:01:41.243 回答