ios 条件绑定:如果让错误 - 条件绑定的初始值设定项必须具有 Optional 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31038759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional Binding: if let error – Initializer for conditional binding must have Optional type
提问by Del Hinds
I am trying to delete a row from my Data Source and the following line of code:
我正在尝试从我的数据源和以下代码行中删除一行:
if let tv = tableView {
causes the following error:
导致以下错误:
Initializer for conditional binding must have Optional type, not UITableView
条件绑定的初始化器必须具有 Optional 类型,而不是 UITableView
Here is the full code:
这是完整的代码:
// Override to support editing the table view.
func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
if let tv = tableView {
myData.removeAtIndex(indexPath.row)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
How should I correct the following?
我应该如何纠正以下问题?
if let tv = tableView {
回答by nhgrif
if let
/if var
optional binding only works when the result of the right side of the expression is an optional. If the result of the right side is not an optional, you can not use this optional binding. The point of this optional binding is to check for nil
and only use the variable if it's non-nil
.
if let
/if var
可选绑定仅在表达式右侧的结果为可选时才有效。如果右侧的结果不是可选的,则不能使用此可选绑定。此可选绑定的重点是检查nil
并仅在非变量时使用该变量nil
。
In your case, the tableView
parameter is declared as the non-optional type UITableView
. It is guaranteed to never be nil
. So optional binding here is unnecessary.
在您的情况下,tableView
参数被声明为非可选类型UITableView
。保证永远不会nil
。所以这里的可选绑定是不必要的。
func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
myData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
All we have to do is get rid of the if let
and change any occurrences of tv
within it to just tableView
.
我们所要做的就是摆脱if let
和改变任何出现tv
在它刚tableView
。
回答by Hymansonkr
for my specific problem I had to replace
对于我的具体问题,我不得不更换
if let count = 1
{
// do something ...
}
With
和
let count = 1
if(count > 0)
{
// do something ...
}
回答by Lehlohonolo_Isaac
In a case where you are using a custom cell type, say ArticleCell, you might get an error that says :
在您使用自定义单元格类型的情况下,例如 ArticleCell,您可能会收到一条错误消息:
Initializer for conditional binding must have Optional type, not 'ArticleCell'
You will get this error if your line of code looks something like this:
如果您的代码行如下所示,您将收到此错误:
if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as! ArticleCell
You can fix this error by doing the following :
您可以通过执行以下操作来修复此错误:
if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as ArticleCell?
If you check the above, you will see that the latter is using optional casting for a cell of type ArticleCell.
如果您检查上述内容,您将看到后者正在对 ArticleCell 类型的单元格使用可选的强制转换。
回答by Paul Tader
Same applies for guardstatements. The same error message lead me to this post and answer (thanks @nhgrif).
同样适用于保护语句。相同的错误消息将我引导到这篇文章并回答(感谢@nhgrif)。
The code: Print the last name of the person only if the middle name is less than four characters.
代码:仅当中间名少于四个字符时才打印该人的姓氏。
func greetByMiddleName(name: (first: String, middle: String?, last: String?)) {
guard let Name = name.last where name.middle?.characters.count < 4 else {
print("Hi there)")
return
}
print("Hey \(Name)!")
}
Until I declared lastas an optional parameter I was seeing the same error.
在我将last声明为可选参数之前,我看到了同样的错误。
回答by Viral Shah
condition binding must have optinal type which mean that you can only bind optional values in if let statement
条件绑定必须具有可选类型,这意味着您只能在 if let 语句中绑定可选值
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
if let tv = tableView as UITableView? {
}
}
}
This will work fine but make sure when you use if let it must have optinal type "?"
这会正常工作,但请确保在您使用 if let 它必须具有可选类型“?”