Search code examples
iosobjective-carraysuiwebview

How to Use Predicate in a Switch Statement


(Apologies if this is an obvious answer, I am very new to programming.)

I have a mutable array defined with different types of fabrics. What I'd like to do is compare a string entered by a user in a text box to the objects in the array. Then, depending on the value of the string, I'll display a different picture in a UIWebView. (The reason I went with arrays was because I read you can't do Switch statements with strings.) So I set up a predicate to search the array.

However, I can't figure out how to go from the Predicate to the Index value of that object, to use in the switch statement.

Should I be following a different tactic? Is this even possible?

-(IBAction)btnAddFabric:(id)sender
 {

  myFabrics = [NSMutableArray arrayWithObjects:@"Cotton",@"Fleece",@"Linen",@"Nylon",
                      @"Polyester",@"Rayon",@"Silk",@"Spandex",@"Suede",@"Wool",nil];
  NSString *fabricType;

  fabricType = self.txtType.text;

  self.lblFabric1.text = fabricType;

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains     
     %d",fabricType];
  NSArray *result = [myFabrics filteredArrayUsingPredicate:predicate];


  switch (result)
   {
     case 0:
      imageURLString=[[NSString alloc] initWithFormat:@"<Picture of cotton fabric from    
         Google here>"];
      break

   }

   [self.fabricPic loadRequest:[NSURLRequest requestWithURL:imageURL];
}

Solution

  • -(IBAction)btnAddFabric:(id)sender
     {
    
      myFabrics = [NSMutableArray arrayWithObjects:@"Cotton",@"Fleece",@"Linen",@"Nylon",
                          @"Polyester",@"Rayon",@"Silk",@"Spandex",@"Suede",@"Wool",nil];
      NSString *fabricType;
    
      fabricType = self.txtType.text;
    
      self.lblFabric1.text = fabricType;
    
      int result=[myFabrics indexOfObject:fabricType];
    
    
      switch (result)
       {
         case 0:
          imageURLString=[[NSString alloc] initWithFormat:@"<Picture of cotton fabric from    
             Google here>"];
          break
    
       }
    
       [self.fabricPic loadRequest:[NSURLRequest requestWithURL:imageURL];
    }