Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the limits of using a ternary operator in Objective-C?

The following Objective-C statement does not work correctly.

cell.templateTitle.text=[(NSDictionary*) [self.inSearchMode?self.templates:self.filteredTemplates objectAtIndex:indexPath.row] objectForKey:@"title"];

However, if I split it into an if() statement it works fine.

if(self.inSearchMode){
  categorize=[(NSDictionary*)[self.filteredTemplates objectAtIndex:indexPath.row] objectForKey:@"categorize"];
} else {
  categorize=[(NSDictionary*)[self.templates objectAtIndex:indexPath.row] objectForKey:@"categorize"]; 
}

What are the limitations of using the ternary operator in Objective-C? In other languages like C# the above ternary statement would have worked correctly.

like image 591
ChrisP Avatar asked Dec 01 '22 23:12

ChrisP


1 Answers

My guess is that it's an order of operations issue. Have you tried:

[(self.inSearchMode?self.templates:self.filteredTemplates) objectAtIndex:indexPath.row]

(notice added parens)

like image 84
cesarislaw Avatar answered May 17 '23 14:05

cesarislaw