2017-04-02 57 views
0

林的代碼下面的工作在發展中國家使用另一種方式擁有objective-C.Is簡單的iOS應用來優化這個代碼:如何優化if-else語句使其更簡單?

if ([segue.identifier isEqualToString:@"showCommitDetail"]) { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    CommitDetailsTableViewController *destViewController = segue.destinationViewController; 

     RepoObject *repoObj = [self.RepoListArray objectAtIndex:indexPath.row]; 
    NSString *repoCommit_url = [repoObj.Commit_url stringByReplacingOccurrencesOfString:@"{/sha}" withString:@""]; 


if (indexPath.row == 0) { 
    NSString *SpringBootURL = repoCommit_url; 
    self.commit_url = SpringBootURL; 
    destViewController.CommitRepoURL = self.commit_url; 
    destViewController.navigationItem.title = @"Spring-Integration-in-Action"; 
}else if (indexPath.row == 1){ 
    NSString *SpringFrameworkURL = repoCommit_url; 
    self.commit_url = SpringFrameworkURL; 
    destViewController.CommitRepoURL = self.commit_url; 
    destViewController.navigationItem.title = @"spring-data-jdbc-ext"; 
}else if (indexPath.row == 2){ 
    NSString *SpringAmqpURL = repoCommit_url; 
    self.commit_url = SpringAmqpURL; 
    destViewController.CommitRepoURL = self.commit_url; 
    destViewController.navigationItem.title = @"spring-data-commons"; 
}else if (indexPath.row == 3){ 
    NSString *SpringIdeURL = repoCommit_url; 
    self.commit_url = SpringIdeURL; 
    destViewController.CommitRepoURL = self.commit_url; 
    destViewController.navigationItem.title = @"spring-data-graph"; 
}else{ 
    NSString *SpringIntegratURL = repoCommit_url; 
    self.commit_url = SpringIntegratURL; 
    destViewController.CommitRepoURL = self.commit_url; 
    destViewController.navigationItem.title = @"spring-data-document-examples"; 
} 

} 

在那旁邊,我怎麼可以直接給標題沒有硬編碼像我上面做過。?

回答

1
if ([segue.identifier isEqualToString:@"showCommitDetail"]) { 
    NSInteger *selectedRow = [self.tableView indexPathForSelectedRow].row; 
    CommitDetailsTableViewController *destViewController = segue.destinationViewController; 

    RepoObject *repoObj = [self.RepoListArray objectAtIndex:selectedRow]; 
    self.commit_url = [repoObj.Commit_url stringByReplacingOccurrencesOfString:@"{/sha}" withString:@""]; 
    destViewController.CommitRepoURL = self.commit_url; 

    NSString *title; 
    switch (selectedRow) { 
     case 0: 
      title = @"Spring-Integration-in-Action"; 
      break; 
     case 1: 
      title = @"spring-data-jdbc-ext"; 
      break; 
     case 2: 
      title = @"spring-data-commons"; 
      break; 
     case 3: 
      title = @"spring-data-graph"; 
      break; 
     default: 
      title = @"spring-data-document-examples"; 
      break; 
    } 
    destViewController.navigationItem.title = title; 
} 
+0

這個答案即時尋找for.thanx爵士幫我找出.... :) –

+0

@abdul haziq 歡迎您! – Terry

-1

您可以嘗試將第二個if else s更改爲以下內容,它將優化代碼行和易讀性。

NSString *url = repoCommit_url; 
self.commit_url = url; 
destViewController.CommitRepoURL = self.commit_url; 

if (indexPath.row == 0) { 
    destViewController.navigationItem.title = @"Spring-Integration-in-Action"; 
}else if (indexPath.row == 1){ 
    destViewController.navigationItem.title = @"spring-data-jdbc-ext"; 
}else if (indexPath.row == 2){ 
    destViewController.navigationItem.title = @"spring-data-commons"; 
}else if (indexPath.row == 3){ 
    destViewController.navigationItem.title = @"spring-data-graph"; 
}else{ 
    destViewController.navigationItem.title = @"spring-data-document-examples"; 
}