조건부 바인딩 : 오류가 발생한 경우 – 조건부 바인딩의 이니셜 라이저에는 선택적 유형이 있어야합니다.
내 데이터 소스와 다음 코드 줄에서 행을 삭제하려고합니다.
if let tv = tableView {
다음 오류가 발생합니다.
조건부 바인딩의 이니셜 라이저에는 UITableView가 아닌 선택적 유형이 있어야합니다.
다음은 전체 코드입니다.
// 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)
다음을 어떻게 수정해야합니까?
if let tv = tableView {
if let
/ if var
선택적 바인딩은 표현식의 오른쪽 결과가 선택적 일 때만 작동합니다. 오른쪽 결과가 선택 사항이 아닌 경우이 선택적 바인딩을 사용할 수 없습니다. 이 선택적 바인딩의 요점은 nil
변수가 아닌 경우에만 확인 하고 사용하는 것 nil
입니다.
귀하의 경우 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)
우리가해야 할 일은를 제거하고 그 안에서 if let
발생하는 모든 tv
것을 tableView
.
내 특정 문제에 대해 교체해야
if let count = 1
{
// do something ...
}
와
let count = 1
if(count > 0)
{
// do something ...
}
ArticleCell과 같은 사용자 정의 셀 유형을 사용하는 경우 다음과 같은 오류가 발생할 수 있습니다.
Initializer for conditional binding must have Optional type, not 'ArticleCell'
코드 줄이 다음과 같으면이 오류가 발생합니다.
if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as! ArticleCell
다음을 수행하여이 오류를 수정할 수 있습니다.
if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as ArticleCell?
위의 내용을 확인하면 후자가 ArticleCell 유형의 셀에 대해 선택적 캐스팅을 사용하고 있음을 알 수 있습니다.
가드 문 에도 동일하게 적용됩니다 . 동일한 오류 메시지가 나를이 게시물로 이끌고 답변합니다 (@nhgrif에게 감사드립니다).
코드 : 중간 이름이 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)!")
}
마지막 으로 선택적 매개 변수로 선언 할 때까지 동일한 오류가 표시되었습니다.
condition binding must have optinal type which mean that you can only bind optional values in if let statement
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 "?"
'Programing' 카테고리의 다른 글
소스 변경시 gunicorn 자동 새로 고침 (0) | 2020.08.13 |
---|---|
파이썬 사전을 csv 파일에 어떻게 작성합니까? (0) | 2020.08.13 |
다른 값을 기준으로 한 벡터를 정렬하는 방법 (0) | 2020.08.13 |
파일 설명자와 파일 포인터의 차이점은 무엇입니까? (0) | 2020.08.13 |
React에서 로컬 이미지를 어떻게 참조합니까? (0) | 2020.08.13 |