gpt4 book ai didi

swift - 在 swift 中,如果没有更多上下文,表达式的类型是不明确的?

转载 作者:行者123 更新时间:2023-11-30 10:30:57 25 4
gpt4 key购买 nike

您好,我有一个学生类(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{$0}.joined(separator: " ").count <= 15
}
}

关于swift - 在 swift 中,如果没有更多上下文,表达式的类型是不明确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com