Programing

조건부 바인딩 : 오류가 발생한 경우 – 조건부 바인딩의 이니셜 라이저에는 선택적 유형이 있어야합니다.

crosscheck 2020. 8. 13. 07:51
반응형

조건부 바인딩 : 오류가 발생한 경우 – 조건부 바인딩의 이니셜 라이저에는 선택적 유형이 있어야합니다.


내 데이터 소스와 다음 코드 줄에서 행을 삭제하려고합니다.

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 "?"

참고URL : https://stackoverflow.com/questions/31038759/conditional-binding-if-let-error-initializer-for-conditional-binding-must-hav

반응형