ios - 尝试以三元表达式返回
问题描述
我有一个 Swift 应用程序。
我Expected expression after '?' in ternary expression
从 Xcode 编译器 收到错误
在
private func getContentPre(highlight: Highlight) -> String!
{
highlight.contentPre.count == 0 ? return ">" : return highlight.contentPre
}
苹果文档说:
为什么不能return
像使用 if 语句那样使用三元表达式?
解决方案
你应该像这样重写你的函数。这将评估contentPre
变量的计数并返回适当的响应。
private func getContentPre(highlight: Highlight) -> String! {
return highlight.contentPre.count == 0 ? ">" : highlight.contentPre
}
但是,您contentPre
应该String
使用.isEmpty
它,因为它比检查 a 的长度更具性能String
private func getContentPre(highlight: Highlight) -> String! {
return highlight.contentPre.isEmpty ? ">" : highlight.contentPre
}
推荐阅读
- r - 查找每个 ID 的重叠日期范围
- ios - 在objective-c中将api响应插入核心数据时逐渐增加内存
- python - 给定作者姓名,显示来自给定 twitter 数据集的 tid 值
- recursion - 程序返回错误“尝试在 0x00000000 处执行非指令”
- ios - 从服务器解析 unicode 字符,iOS Swift
- java - 为什么 isEmpty 函数不适用于我的代码?
- android - 读取超过 130000 行文本的大文本文件
- c# - 如何通过“Microsoft 打印到 PDF”以编程方式将文件和网页打印到 PDF?
- intellij-idea - WebStorm 和 IntelliJ 的作用是什么?
- scala - 如何在具有多个工作表的 IntellijIDEA Scala 项目中只运行一个工作表?