Ios 动态开关语句:Objective-C
我有一系列类别:Ios 动态开关语句:Objective-C,ios,objective-c,arrays,switch-statement,Ios,Objective C,Arrays,Switch Statement,我有一系列类别: ["main", "category1", "category2", "category3"] 当前,我执行以下操作来设置选中时每个列表项的url - (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index{ NSString *subCategoryURL = @""; tapCellIndex = -1
["main", "category1", "category2", "category3"]
当前,我执行以下操作来设置选中时每个列表项的url
- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index{
NSString *subCategoryURL = @"";
tapCellIndex = -1;
switch (index) {
case 1: {
subCategoryURL = [NSString stringWithFormat:@"%@%@/",CATEGORYURL,@"category1"];
}
break;
case 2: {
subCategoryURL = [NSString stringWithFormat:@"%@%@/",CATEGORYURL,@"category2"];
}
break;
case 3: {
subCategoryURL = [NSString stringWithFormat:@"%@%@/",CATEGORYURL,@"category3"];
}
break;
case 0: {
isMenuChoosed = NO;
[self getHomePageDetails];
return;
}
break;
default: {
subCategoryURL = [NSString stringWithFormat:@"%@%@/",CATEGORYURL,@"category1"];
}
break;
}
CategoryURL = subCategoryURL;
[self getCategoryDeatils:subCategoryURL];
}
这一切照常进行。但是,我的类别可能会更改在列表中的位置,并且可能会添加或删除新类别,因此我需要switch语句是动态的
例如,案例数需要是数组中项目的数量,
categoryX
(以url结尾)需要是数组中类别的名称
有人能帮我做这个吗
allCategories = @["main", "category1", "category2", "category3"]
如果根据此数组创建按钮,则可以执行以下操作
- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index{
NSString *subCategoryURL = @"";
if(![allCategories[index] isEqualToString:@"main"])
{
subCategoryURL = [NSString stringWithFormat:@"%@%@/",CATEGORYURL,allCategories[index]];
}
else
{
//main here
return;
}
CategoryURL = subCategoryURL;
[self getCategoryDeatils:subCategoryURL];
}
“例如,案例数需要是数组中的项目数”,则不能使用
开关语句。switch
语句需要在编译时完全写出,但在运行时之前您不知道类别的数量。@matt还有其他解决方案吗?