2017-02-02 7 views
9

मैं सिर्फ स्विफ्ट के नवीनतम संस्करण के लिए अपने ऐप्लिकेशन के कोड अद्यतन और मैं इस समारोह है:यूनरी ऑपरेटर '-' प्रकार के ऑपरेंड पर लागू नहीं किया जा सकता है '@lvalue Int?' (उर्फ '@lvalue वैकल्पिक <Int>')

func setupGraphDisplay() { 

     //Use 7 days for graph - can use any number, 
     //but labels and sample data are set up for 7 days 
     //let noOfDays:Int = 7 

     //1 - replace last day with today's actual data 
     graphView.graphPoints[graphView.graphPoints.count-1] = counterView.counter 

     //2 - indicate that the graph needs to be redrawn 
     graphView.setNeedsDisplay() 

     maxLabel.text = "\((graphView.graphPoints).max()!)" 
     print((graphView.graphPoints).max()!) 

     //3 - calculate average from graphPoints 
     let average = graphView.graphPoints.reduce(0, +) 
      /graphView.graphPoints.count 
     averageWaterDrunk.text = "\(average)" 

     //set up labels 
     //day of week labels are set up in storyboard with tags 
     //today is last day of the array need to go backwards 

     //4 - get today's day number 
     //let dateFormatter = NSDateFormatter() 
     let calendar = Calendar.current 
     let componentOptions:NSCalendar.Unit = .weekday 
     let components = (calendar as NSCalendar).components(componentOptions, 
      from: Date()) 
     var weekday = components.weekday 

     let days = ["S", "S", "M", "T", "W", "T", "F"] 

     //5 - set up the day name labels with correct day 
     for i in (1...days.count).reversed() { 
      if let labelView = graphView.viewWithTag(i) as? UILabel { 
       if weekday == 7 { 
        weekday = 0 
       } 
       labelView.text = days[(weekday--)!] 
       if weekday! < 0 { 
        weekday = days.count - 1 
       } 
      } 
     } 
    } 

हालांकि मैं निम्न पंक्ति पर एक त्रुटि संदेश मिल रहा है:

labelView.text = days[(weekday--)!] 

कहाँ Xcode मुझे निम्न त्रुटि दे रहा है:

Unary operator '--' cannot be applied to an operand of type '@lvalue Int?' (aka '@lvalue Optional<Int>') 

मैं जवाब के लिए ऑनलाइन खोज की कोशिश की लेकिन मैं अभी भी कर सकते थे कुछ भी नहीं मिला जो मुझे इस त्रुटि को ठीक करने में मदद करेगा।

मैं भी उत्सुक था कि @lvalue Int (aka '@lvalue Optional<Int>') टाइप करके त्रुटि संदेश का क्या अर्थ है? मैंने पहले कभी इस डेटा प्रकार को नहीं देखा है और यह नहीं पता कि तदनुसार समस्या को कैसे हल किया जाए।

अग्रिम धन्यवाद।

+5

'--' और' ++ 'को स्विफ्ट से हटा दिया गया है। उपरोक्त रेखा पर 'सप्ताहांत - = 1' का प्रयोग करें। – par

+0

आपको सी-स्टाइल वृद्धि/कमी, लूप के लिए सी-स्टाइल का उपयोग करना बंद करना सीखना होगा – Pierce

उत्तर

14

उत्तर बहुत आसान है। ++ और - स्विफ्ट 3 से हटा दिया गया था। लेकिन + = और - =

लगभग Optional<Int> यह Int? परिभाषा के लिए लंबा संस्करण है। स्विफ्ट वैकल्पिक रूप से public enum Optional<Wrapped> : ExpressibleByNilLiteral

संबंधित मुद्दे