조건부 바인딩: if let error – 조건부 바인딩 이니셜라이저에는 옵션 유형이 있어야 합니다.
데이터 소스와 다음 코드 행에서 행을 삭제하려고 합니다.
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 varoptional binding은 식 오른쪽의 결과가 옵션인 경우에만 기능합니다.오른쪽의 결과가 옵션이 아닌 경우 이 옵션바인딩을 사용할 수 없습니다.이 옵션 바인딩의 요점은 다음 항목을 확인하는 것입니다.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 타입의 셀에 옵션 캐스팅을 사용하고 있음을 알 수 있습니다.
guard 스테이트먼트도 마찬가지입니다.같은 에러 메세지가 표시되고, 이 투고와 회답이 표시됩니다(고맙습니다 @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)!")
}
옵션 파라미터로 last를 선언할 때까지 같은 에러가 발생하고 있었습니다.
조건 바인딩에는 optinal 타입이 필요합니다.즉, 옵션 값은 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? {
}
}
}
이것은 정상적으로 동작합니다만, 옵티널 타입의 「?」를 사용할 필요가 있는 경우는 반드시 확인해 주세요.
음, if 조건 내에서 일반적인 값을 선언할 수 있다면 여전히 편리할 것입니다.여기 요령이 있습니다: 컴파일러가 이 명령어를 할당받았다고 생각하게 할 수 있습니다.Optional.some(T)다음과 같은 값으로 변환합니다.
if let i = "abc".firstIndex(of: "a"),
let i_int = .some(i.utf16Offset(in: "abc")),
i_int < 1 {
// Code
}
그것이 의미하는 것은 -2nd guard let또는if let check는 Optional Int 또는 Optional String에서는 발생하지 않습니다.이미 선택사항이 아닌 값이 있으므로 더 이상 가드 또는 if-leting이 필요하지 않습니다.
텍스트 필드를 확인하는 경우 옵션 텍스트를 확인하시겠습니까?
guard let msg = message.text?.trimmed,!messaged.isEmpty,messaged.count >= 14 else {
MyVariables.status.image = UIImage(named: "wrong")
MyVariables.status.message = "Enter Your Message".localized
MyVariables.status.showInKeyWindow()
return
}
언급URL : https://stackoverflow.com/questions/31038759/conditional-binding-if-let-error-initializer-for-conditional-binding-must-hav
'programing' 카테고리의 다른 글
| Python이 실행되고 있는 OS를 식별하는 방법 (0) | 2023.04.19 |
|---|---|
| SQL Server에서 사용자를 생성한 후 로그인할 수 없음 (0) | 2023.04.19 |
| 부트스트랩3의 sr-only는 무엇입니까? (0) | 2023.04.19 |
| 여러 줄 셸 변수를 지정하려면 어떻게 해야 합니까? (0) | 2023.04.19 |
| Swift에서 dispatch_once singleton 모델 사용 (0) | 2023.04.19 |