2015-03-08 7 views
13

Chyba jestem patrząc na niektóre przestarzałego kodu: «?»szybka? muszą być przestrzegane przez wywołanie, członek odnośnika lub indeksu

@IBAction func stockLevelDidChange(sender: AnyObject) { 
     if var currentCell = sender as? UIView { 
      while (true) { 
       currentCell = currentCell.superview!; 
       if let cell = currentCell as? ProductTableCell { 
        if let id = cell.productId? { 
         var newStockLevel:Int?; 
         if let stepper = sender as? UIStepper { 
          newStockLevel = Int(stepper.value); 
         } 
         else if let textfield = sender as? UITextField { 
          if let newValue = textfield.text.toInt()? { 
           newStockLevel = newValue; 
          } 
         } 
         if let level = newStockLevel { 
          products[id].4 = level; 
          cell.stockStepper.value = Double(level); 
          cell.stockField.text = String(level); 
         } 
        } 
       break; 
       } 
      } 
      displayStockTotal(); 
     } 
    } 

Ale w pierwszej linii funkcji otrzymuję " musi następować połączenie, wyszukiwanie członków lub indeks dolny "(znak zapytania po as)

Co oznacza ten błąd i jak zmienia się ten kod w Swift 1.2?

Odpowiedz

17

W rzeczywistości as? są w porządku. Problem jest następujący:

if let id = cell.productId? 

Wystarczy usunąć znak zapytania na końcu tego. To nie ma sensu.

+0

To zadziałało. Ale dlaczego Xcode pokazuje, że błąd dotyczy "if var currentCell = sender"? Linia UIView'? – soleil

0

Problem to if let newValue = textfield.text.toInt()? { .. Jeśli toInt() zwróci Int?, po prostu pozbądź się ? tam.

+3

Ciekawe jest to tylko błąd w Swift 1.2. W Swift 1.1 to faktycznie kompiluje i działa. –

+0

jaki jest powód dla tego języka? Mam ten sam błąd dla '' 'if (self.cat?! = Nil) {return self}' '' – loretoparisi

0

W wersji 1.2, toInt nie ma. Więc

if let newValue = textfield.text.toInt()? 

należy wymienić:

if let newValue:Int? = Int(textField.text!) 
Powiązane problemy