0
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    0
}

Why does compiler does not throw error even when return keyword is not added while returning built-in data types ?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    UITableViewCell()
}

Whereas in case of non built in types it throws error : Missing return in a function expected to return

Harshit
  • 103
  • 1
  • 4

1 Answers1

0

Swift 5.1 added the ability to omit the return keyword for functions with a single-expression. See https://github.com/apple/swift-evolution/blob/master/proposals/0255-omit-return.md

It's got nothing to do with built-in data types vs. non-built-in; in fact I'm not even sure what you mean by that.

If you get the error it's because your function is no longer a single expression.

// This compiles
func foo() -> Int {
  42
}

func bar() -> Int {
  #warning("This won't compile")
  42
}
Rudedog
  • 4,323
  • 1
  • 23
  • 34