AskMe
AskMe

Reputation: 2583

How to extract value from NSMutable array and put it in NSArray?

I have NSMutableArray like following:

In .h

@property (nonatomic,retain) NSMutableArray *arrReceipes; 

In .m

@synthesize arrReceipe;   

Inside a method

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];

That is "arrReceipes" is my NSMutable array.

Now I want to extract the value/string of "arrReceipes" and put it into "NSArray * somevariable".

How can I do this?

This I' doing for :

-(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];
    }
}

In my 3rd line above(in 'if') if I'm giving "arrReceipes" its throwing exception in "main" method.But, if, instade of giving "arrReceipes" I'll give "somevariable" its working fine.

Hope you understand my problem.Please help.Thanks.

Upvotes: 0

Views: 156

Answers (3)

k20
k20

Reputation: 2008

You can try with

NSArray *somevariable = [NSArray arrayWithArray:arrReceipes];

NSMutableArray is a subclass of an NSArray so you can just use NSArray methods

Upvotes: 0

ppaulojr
ppaulojr

Reputation: 3647

You can't write in an NSArray.

However you can do something like this:

NSArray * somevariable = [arrReceipes copy];

Also you can modify your code to write the data into a NSMutableArray and then copy it to arrRecipes

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31026

The objects that you're putting into arrReceipes are dictionaries, so the destViewController.recipeName should be a dictionary if you want to pass it one of those objects.

Upvotes: 1

Related Questions