如何从NSMutablearray中提取值并将其放入NSArray中?

时间:2012-12-05 14:56:20

标签: iphone objective-c ios cocoa

我有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'中)如果我给“arrReceipes”它在“main”方法中抛出异常。但是,如果,给出“arrReceipes”的instade我会给“somevariable”它的工作细

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你无法写入NSArray。

但是你可以这样做:

NSArray * somevariable = [arrReceipes copy];

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

答案 2 :(得分:0)

您可以尝试使用

NSArray *somevariable = [NSArray arrayWithArray:arrReceipes];

NSMutableArray是NSArray的子​​类,因此您只需使用NSArray方法