Depending on my understanding of guard
statement in swift I am doing the following:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let identifier = "identifier"
let dequeCell = tableView.dequeueReusableCellWithIdentifier(identifier)
guard let cell = dequeCell else
{
// The following line gives error saying: Variable declared in 'guard'condition is not usable in its body
cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)
}
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
I just want to understand is can we create a variable in guard
statement and access it in rest of the function? or are the guard statements intended to immediately return or throw exceptions?
Or I am completely misunderstanding the usage of guard
statement?