-1

i've implemented this switch statement within my code, and it worked perfectly fine until i restarted Xcode. Now it hits me with this error:

enter image description here

I clearly declared the variable in the above line of code. This was not a problem a couple of minutes ago. Is this just Xcode acting up?

Robert Tillman
  • 993
  • 2
  • 10
  • 20
  • The thing is: swift wants variables to have a state before the object is used. You declared a var vc of type UITableViewController only. Theres no value for that. And the switch statement may never get called. So we have a situation where vc might never have a value. Try to put var vc:UITableViewController? – kandelvijaya May 17 '15 at 10:10
  • i set it to var vc:UITableViewController? and in turn set vc to vc! in my pushViewController method & it crashed @kandelvijaya – Robert Tillman May 17 '15 at 10:23
  • What do you expect if indexPath.row is not zero? You don't assign a view controller to vc in that case, of course that will crash. – Martin R May 17 '15 at 10:24
  • i've ran the app in the debugger & in fact the indexPath.row of the cell i am testing is 0. @MartinR – Robert Tillman May 17 '15 at 10:27
  • if you have multiple sections then its a problem i guess. – kandelvijaya May 17 '15 at 10:40
  • How many sections do you have? – kandelvijaya May 17 '15 at 10:41
  • Compare http://stackoverflow.com/questions/30189505/missing-return-uitableviewcell/30190231#30190231 for a similar problem and how you can utilize fatalError() to tell the compiler about the "impossible" cases. – Martin R May 17 '15 at 10:43
  • just 1 @kandelvijaya – Robert Tillman May 17 '15 at 18:57
  • i utilized fatalError() within my default: & it crashed as soon as i selected the 1st row in my table view @MartinR – Robert Tillman May 17 '15 at 22:31
  • do i need to return a value? or use an if statement? what am i missing @MartinR – Robert Tillman May 17 '15 at 22:34

1 Answers1

2

It doesn't say your variable is undeclared. It says that it is not initialized. And it's right. I don't really know why did it work before. It shouldn't have.

The problem is that if indexPath.row is not equal to 0, the case 0 line will not be performed. The default case doesn't set any value to vc. So, you don't assign anything to vc, and yet you pass it into a function.

In order to fix it you should either set some default value to vc or set value in the default section.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60