Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

objective c - Warning: control reaches end of non-void function - iPhone

I keep getting 'warning: control reaches end of non-void function' with this code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 if (section ==0)

 {
  return [comparativeList count];
 }

 if (section==1) 
 {
  return [generalList count];
 }

 if (section==2)
 {

  return [contactList count];

How can I get rid of this warning?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add a return 0; at the end of your method. It's basically a failsafe, if none of the if conditions are met.

If you want to make sure one of the conditions is met, return -1; should cause the application to throw an exception and crash, which might help you track down errors.

You might also look at modifying this method and replacing the if statements with a switch-case tree. With a switch-case tree, you can very easily add new sections and rearrange their order in a UITableView. The code becomes trivially easy to read by using sensible naming conventions.

It's really easy; Fraser Speirs has a good explanation on how to set this up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...