Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift ? must be followed by a call, member lookup, or subscript

Tags:

swift

I think I'm looking at some outdated code:

@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();
        }
    }

But in the first line of the function I get " '?' must be followed by a call, member lookup, or subscript" (for the question mark after as)

What does this error mean and how does this code change for Swift 1.2?

like image 604
soleil Avatar asked Mar 08 '15 20:03

soleil


2 Answers

Actually the as? are all fine. The problem is this line:

if let id = cell.productId?

Just remove the question mark at the end of that. It makes no sense.

like image 138
matt Avatar answered Sep 19 '22 21:09

matt


In 1.2, toInt is gone. So,

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

Should be replaced with:

if let newValue:Int? = Int(textField.text!)
like image 29
psp Avatar answered Sep 22 '22 21:09

psp