在 swift 中,表达式类型在没有更多上下文的情况下不明确?

In swift Type of expression is ambiguous without more context?

你好我有学生 class 有一些细节,下面是代码

struct Student {
   let stud_name1: String?
   let stud_name2: String?

   var length: Bool {
       return[stud_name1, stud_name2].joined(separator: " ").count <= 15
 }
}

我想检查有效长度,两个名称都是可选的。这是错误消息:

"Type of expression is ambiguous without more context?"

下面是产生此错误的行:

return[stud_name1,stud_name2].joined(separator: " ").count <= 15 

我不确定到底发生了什么,如果有人 可以帮帮我,谢谢。

使其结构化

struct Student {
   let stud_name1: String
   let stud_name2: String 
   var length: Bool {
       return[stud_name1, stud_name2].joined(separator: " ").count <= 15
   }
}

或使用compactMap

struct Student {
   let stud_name1: String?
   let stud_name2: String?
   var length: Bool {
    return[stud_name1, stud_name2].compactMap{[=11=]}.joined(separator: " ").count <= 15
   }
}

您使值计算过于复杂。此代码的作用相同,但效率更高。

struct Student {
   let stud_name1: String?
   let stud_name2: String?

   var length: Bool {
       let name1len = stud_name1?.length ?? 0
       let name2len = stud_name2?.length ?? 0
       return name1len + 1 + name2len <= 15
   }
}