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
条件绑定的初始化器必须具有可选类型,而不是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 {
5 个解决方案
#1
154
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
.
如果允许/如果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。它保证永远不会是零。所以这里的可选绑定是不必要的。
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。
#2
12
for my specific problem I had to replace
对于我的具体问题,我必须替换
if let count = 1
{
// do something ...
}
With
与
let count = 1
if(count > 0)
{
// do something ...
}
#3
7
Same applies for guard statements. 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.
代码:只在中间名小于4个字符时打印此人的姓。
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 last as an optional parameter I was seeing the same error.
直到我将last声明为可选参数,我才看到相同的错误。
#4
4
condition binding must have optinal type which mean that you can only bind optional values in if let statement
条件绑定必须具有optinal类型,这意味着您只能在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 "?"
这将会很好地工作,但确保当你使用时,如果让它必须有光学类型“?”
#5
4
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类型的单元格使用可选的强制转换。
#1
154
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
.
如果允许/如果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。它保证永远不会是零。所以这里的可选绑定是不必要的。
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。
#2
12
for my specific problem I had to replace
对于我的具体问题,我必须替换
if let count = 1
{
// do something ...
}
With
与
let count = 1
if(count > 0)
{
// do something ...
}
#3
7
Same applies for guard statements. 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.
代码:只在中间名小于4个字符时打印此人的姓。
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 last as an optional parameter I was seeing the same error.
直到我将last声明为可选参数,我才看到相同的错误。
#4
4
condition binding must have optinal type which mean that you can only bind optional values in if let statement
条件绑定必须具有optinal类型,这意味着您只能在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 "?"
这将会很好地工作,但确保当你使用时,如果让它必须有光学类型“?”
#5
4
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类型的单元格使用可选的强制转换。