views:

79

answers:

2

how can i ask an array if it contains an item and if it does it to [[NSArray alloc] initWithObjects:@"those objects" automatically. this is my fav .h

@interface FavoriteViewController : UITableViewController {
    NSMutableArray *favoritesArray;
    NSMutableArray *didContain;
}
@property (nonatomic, retain) NSMutableArray *favoritesArray;

@property (nonatomic, retain) NSMutableArray *didContain;

this is the .m

 favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];

if 
([favoritesArray containsObject:@"one"])
{

    [didContain addObject:@"one"];
}

and in the detail view controller.m i have this code

[[NSMutableArray alloc] init];
[favoritesArray addObject: @"one"];

i get the table to work however nothing shows up....

A: 
  NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  NSMutableArray *didContain = [[NSArray alloc] init];


  if([yourArray contains: @"Hello"])
{
        [didContain addObject:@"Hello"];
}

or

  NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  NSMutableArray *didContain = [[NSArray alloc] init];

  [didContain addObject: [yourArray objectAtIndex:[yourArray indexOfObject:@"Hello"]];

All of this and more is readily available in the apple docs. Please do some google searching first next time. Good luck hope this helps.

E. Criss
However, you'll need to tweak the code above if your array contains more than one of the objects you're looking for.
Jordan
for the first example i would need to use didContain in the table view data or "your Array".i woud tweak it by adding else if correct?
Alx
you're honestly going to have to use a looping structure and probably declare another array of the objects you're looking for. Loop through the array containing objects you're looking for seeing if they're contained in "yourArray" and if they are then copy them to "didContain"
E. Criss
A: 

Use filteredArrayUsingPredicate: See NSArray Class Reference and Predicate Programming Guide

It appears that you are trying to use an uninitialized property in your detail view controller.

Normally, you initialize properties in your init: or viewDidLoad method implementations then before presenting the view in your parent view controller, set the property using the property accessors

This line:

// DetailViewController.m initializer code
[[NSMutableArray alloc] init]; // returned object is not used

Should be:

 favoritesArray = [[NSMutableArray alloc] init]; // view controller initialization code

Instead of calling this:

 [favoritesArray addObject:@"one"]; 

After you create your detailViewController set the favoritesArray with the filtered array

// FavoriteViewController.m 
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];

// populate the array
[dvc setFavoritesArray:didContain];

// Assuming you are using a navigation controller

[navigationController pushViewController:dvc animated:YES];
[dvc release];
falconcreek
great this helps however how i have an array that adds different objects in different places and i cant seem to get it to load the items. i used the addobject method in my button to add an item to the array when a button is clicked. so how can i load the array intact without initwithobjects?
Alx
you will have to show your code.
falconcreek
Sry for the bad formating I'm on an iPhone here it is objective c i have an ibaction with code [[NSMutableArray alloc] init]; [favoritesArray addObject: @"one"]; //and in the fav table view this code// favoritesArray = [[NSMutableArray alloc]init]; didContain = [[NSMutableArray alloc]init]; if ([favoritesArray contains:@"one"]); { [didContain addObject:@"trial"]; } however its crashing at the if part...
Alx
Can you update the question with your code instead of using comments? Include the header and full method implementations.
falconcreek