(抱歉,如果这是一个明显的答案,我对编程很陌生。)
我有一个用不同类型的织物定义的可变数组。我想做的是将用户在文本框中输入的字符串与数组中的对象进行比较。然后,根据字符串的值,我将在 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/