首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从NSMutable数组中提取值并将其放入NSArray?

如何从NSMutable数组中提取值并将其放入NSArray?
EN

Stack Overflow用户
提问于 2012-12-05 22:56:20
回答 3查看 144关注 0票数 0

我的NSMutableArray如下所示:

在.h中

代码语言:javascript
运行
复制
@property (nonatomic,retain) NSMutableArray *arrReceipes; 

在.m中

代码语言:javascript
运行
复制
@synthesize arrReceipe;   

在方法内部

代码语言:javascript
运行
复制
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”中。

我该怎么做呢?

我这么做是为了:

代码语言:javascript
运行
复制
-(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];
    }
}

在上面的第三行(在‘if’中),如果我在"main“method.But中给了"arrReceipes”抛出异常,如果我给了"arrReceipes“,我会给"somevariable”它的工作状态。

希望你能理解我的problem.Please help.Thanks。

EN

回答 3

Stack Overflow用户

发布于 2012-12-05 23:00:59

您放入arrReceipes的对象是字典,所以如果您想向destViewController.recipeName传递这些对象中的一个,那么它应该是一个字典。

票数 1
EN

Stack Overflow用户

发布于 2012-12-05 23:01:41

您不能在NSArray中编写代码。

但是,您可以这样做:

代码语言:javascript
运行
复制
NSArray * somevariable = [arrReceipes copy];

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

票数 0
EN

Stack Overflow用户

发布于 2012-12-06 02:11:36

您可以尝试使用

NSArray *somevariable = [NSArray arrayWithArray:arrReceipes];

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13725964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档