ios - 如何在 switch 语句中使用谓词

标签 ios objective-c arrays uiwebview

(抱歉,如果这是一个明显的答案,我对编程很陌生。)

我有一个用不同类型的织物定义的可变数组。我想做的是将用户在文本框中输入的字符串与数组中的对象进行比较。然后,根据字符串的值,我将在 UIWebView 中显示不同的图片。 (我使用数组的原因是因为我读到你不能用字符串执行 Switch 语句。)所以我设置了一个谓词来搜索数组。

但是,我不知道如何从 Predicate 转到该对象的 Index 值,以便在 switch 语句中使用。

我应该采用不同的策略吗?这甚至可能吗?

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

最佳答案

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

关于ios - 如何在 switch 语句中使用谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22491413/

相关文章:

ios - 排序顺序与显示排序顺序

iphone - 混音控制台应用程序中的多个平移手势

objective-c - imessage 扩展程序可以打开主机应用程序吗?

javascript - 为什么将 0 作为数组的第一个元素会阻止 "for"循环执行。 [JavaScript]

iphone - 为什么 CLLocationManager 总是返回 true?

iphone - 允许用户交互iphone sdk吗?

iphone - 我如何知道我的应用程序即将变为非事件状态/进入后台状态?

ios - 单击按钮后获取 uitableview 的复选标记值

c - 数组复制循环使每个元素都相同

使用函数(频率)计算字符数