Xcode4 如何使Else-If语句更适合Xcode 4 iOS
我有一个很长的else-if语句,其中包含一些链接和一些文本。 这是针对Xcode 4、iOS 5的 编辑:Xcode4 如何使Else-If语句更适合Xcode 4 iOS,xcode4,ios5,if-statement,tableview,Xcode4,Ios5,If Statement,Tableview,我有一个很长的else-if语句,其中包含一些链接和一些文本。 这是针对Xcode 4、iOS 5的 编辑: stationList = [[NSMutableArray alloc] init]; [stationList addObject:@"Q-dance Radio"]; [stationList addObject:@"The Voice"]; (ect ...) [stationList sortUsingFunction:compareLe
stationList = [[NSMutableArray alloc] init];
[stationList addObject:@"Q-dance Radio"];
[stationList addObject:@"The Voice"];
(ect ...)
[stationList sortUsingFunction:compareLetters context:nil];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[stationList objectAtIndex:indexPath.row] isEqual:@"Q-dance Radio"])
{
[player pause];
NSString *u = @"LINK TO Q DANCE RADIO";
NSURL *url = [NSURL URLWithString:u];
player = [[AVPlayer alloc] initWithURL:url];
[player play];
}else if{
[player pause];
NSString *u = @"LINK TO THE VOICE";
NSURL *url = [NSURL URLWithString:u];
player = [[AVPlayer alloc] initWithURL:url];
[player play];
} (ect ...)
如何使其更好或更快地找到按下的一个,这样它就不需要运行整个列表。您可以使用NSDictionary,并使用静态字符串作为键。我想你会得到一个很好的加速。(字典查找可能会对用作键的输入字符串进行哈希运算。) 如果你提供更多的细节,我可能会提供一个更彻底的答案。例如,您甚至可以将“Dosomethine here”代码存储为^{}样式的块 更新 我现在在我的电脑前,可以为你参考一些真实的代码。这是基于我已经做过的事情。您的代码可以这样修改:
xxxList = [[NSMutableArray alloc] init];
xxxListActionDictionary = [[NSMutableDictionary alloc] initWithCapacity:10]; // add this line
[xxxList addObject:@"TEXT HERE"];
[xxxListActionDictionary
setObject:
[[^{
// DO SOMETHING HERE. THIS IS A BLOCK. SOME CODE TO BE EXECUTED.
} copy] autorelease] // I don't think you need autorelease if you use ARC
forKey:@"TEXT HERE"]; // TEXT HERE is the same text put into the xxxListArray
ect.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Here is where it gets efficient.
// The next three lines of code replace the entire if-else if...
void (^action)(void) = [xxxActionDictionary objectForKey:[[xxxList objectAtIndex:indexPath.row]];
if (action != nil) // you want to make sure the key is in the dictionary. may not be needed in your case.
action(); // this executes the block
更新
stationList = [[NSMutableArray alloc] init];
[stationList addObject:@"Q-dance Radio"];
[stationListDictionary setObject:@"http://whateverQDanceRadioIs.com/folder/..." forKey:@"Q-dance Radio"];
[stationList addObject:@"The Voice"];
[stationListDictionary setObject:@"http://whateverTheVoiceIs.com/folder/..." forKey:@"Q-dance Radio"];
(ect ...)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[player pause];
NSString *u = [stationListDictionary objectForKey[stationList objectAtIndex:indexPath.row]];
NSURL *url = [NSURL URLWithString:u];
player = [[AVPlayer alloc] initWithURL:url];
[player play];
谢谢!我在atm机上工作,所以我回家后会发布一些更详细的代码,回家后也会尝试修改。我已经更新了我的代码,所以你可以看到我在做什么。。我想是不是最好制作一个包含所有链接和站点名称的plist,然后通过choosen ID或其他方式播放链接?或者你的代码还在这里工作?你可以这么做。If看起来像URL字符串是每个If块中唯一的区别。你很可能会把plist读入词典。关于如何做到这一点,有很多例子。或者你也可以硬编码字典的内容。(这就是我要做的,因为我讨厌编辑plist!)好吧,那我就别管plist了:p这样我仍然可以使用你的代码来编辑我为我做的东西?我添加了一个更新,应该会有所帮助。我最初的答案是通用的,但你不需要一个非常健壮的。但字典是一个很好的方法来做你想做的事。