2017-04-08 4 views
4

이 경고는 내가 염려해야 할 사항입니까? 'Int'로 'NSNumber'브리징 경고

warning

해결책이 될 것입니다 무슨 그렇다면? 이 제 기능이다

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let destination = segue.destination as? ProfileViewController{ 
    let cell = sender as! UITableViewCell 
     let selectedRow = myTableView.indexPath(for: cell)!.row 


     switch (mySegmentedControl.selectedSegmentIndex){ 
     case 0: 
      destination.nameVar = userSFList[selectedRow].name! 
      destination.imageOneURL = userSFList[selectedRow].image! 
      destination.bioVar = userSFList[selectedRow].bio! 

      if let image2 = userSFList[selectedRow].imageTwo { 
       destination.imageTwoUrl = image2    } 

      if let contactInt = userSFList[selectedRow].contact as? Int { 
       destination.contact = contactInt 
      } 

      break 

     case 1: 

      destination.nameVar = userEBList[selectedRow].name! 
      destination.imageOneURL = userEBList[selectedRow].image! 
      destination.imageTwoUrl = userEBList[selectedRow].imageTwo! 



      if let contactInt = userEBList[selectedRow].contact as? Int { 
       destination.contact = contactInt 
      } 


      break 
     case 2: 
      destination.nameVar = userSFOList[selectedRow].name! 
      destination.imageOneURL = userSFOList[selectedRow].image! 

      if let contactInt = userSFOList[selectedRow].contact as? Int { 
       destination.contact = contactInt 
      } 

      break 
     case 3: 
      destination.nameVar = userSJList[selectedRow].name! 
      destination.imageOneURL = userSJList[selectedRow].image! 
        if let contactInt = userSJList[selectedRow].contact as? Int { 
       destination.contact = contactInt 
      } 
      break 
     default: 
      break 

    } 
} 

}

I 4 개 가지 부분으로 분할 된 제어를 사용 중포 기지를 사용하여 데이터를 인상하고있다.

+1

다음과 같이하십시오.'destination.contact = userEBList [selectedRow] .contact.intV alue'; 옵션이 아니라면'destination.contact = userEBList [selectedRow] .contact.intValue ??와 같은 것을 할 필요가있을 것입니다. 0 '(즉, 디폴트는 0)이다. –

+0

동일한 코드가 3 번 반복되는 이유는 무엇입니까? – Alexander

+0

@Alexander 그것은 userEBList, userSJList 및 userSFOList의 세 가지 경우처럼 보입니다. –

답변

4

개인 규칙은 입니다. 항상경고가 없습니다.입니다.
미안보다 안전합니다.

contactOptional입니까?

if let contactInt = userSFOList[selectRow].contact as? Int { 
    destination.contact = contactInt 
} 

또는 Nil-Coalescing Operator : Optional Binding

당신은 사용할 수 있습니다 ... 그렇다면

destination.contact = userSFOList[selectedRow].contact.intValue ?? <Your default Int here> 

또한 guard를 사용할 수처럼 @ Kamil.S 지적과 같이

guard let nameVar = userSFOList[selectedRow].name, 
    let imageVar = userSFOList[selectedRow].image, 
    let contactVar = contact as? Int else { 
    // Conditions were failed. `return` or `throw`. 
    } 

destination.nameVar = nameVar 
destination.imageOneURL = imageVar 
destination.contact = contactVar 
+0

그런 유형 불일치가 실패하면 우아한 가드를위한 좋은 후보가 될 것입니까? –

+1

@ Kamil.S 나는 그것을 좋아한다. 나는 내 대답을 당신의 제안으로 업데이트했다. –