Swift function compiler error 'missing return'

后端 未结 3 1832
余生分开走
余生分开走 2021-01-23 04:58

I\'ve been trying to get this function to return a Bool value but I don\'t understand why i\'m getting the error \"missing return in a function expected to return \'Bool\'. I\'v

3条回答
  •  时光取名叫无心
    2021-01-23 05:30

    Your function does not handle case where b is an empty array. You need to define what you want the return value to be for such case, because your loop will be skipped when b is an empty array.

    Secondly, your logic is also incomplete, because if the condition is good for i==0, you immediately return true, without checking the rest of the items.

    Thirdly, you probably want to make sure a and b have same length.

    So here is what your function should look like:

    func trueSquare(a:[Int], b:[Int]) -> Bool {
        if a.count != b.count {
            return false
        }
        for i in 0..

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题