2017-02-02 34 views
9

Sadece Swift son sürümüne benim uygulama kodunu güncel ve ben bu işlevi vardır:Tek operatör '-', '@lvalue Int?' Türünde bir işlenene uygulanamaz. (Aka '@lvalue Opsiyonel <Int>')

Ben aşağıdaki satırda bir hata mesajı alıyorum Ancak
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 bana aşağıdaki hata veriyor

:

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

cevapları çevrimiçi arama denedim ama hala could Bu hatayı düzeltmeme yardımcı olacak hiçbir şey bulamadım.

Ayrıca hata iletisinin tam olarak @lvalue Int (aka '@lvalue Optional<Int>') türüne göre ne anlama geldiğini merak ediyordum? Bu veri türünü daha önce hiç görmedim ve sorunu nasıl çözeceğimi bilmiyorum.

Şimdiden teşekkürler.

+5

'-' ve '++', Swift'den kaldırılmıştır. Hemen yukarıdaki satırda 'weekday - = 1' kullanın. – par

+0

C stili artış/azaltmayı kullanmayı da öğrenmeniz gerekecek, ayrıca döngüler için C stili – Pierce

cevap

14

Cevap çok basit. ++ ve - Swift 3'ten kaldırıldı. Fakat + = ve - = kaldı

Optional<Int> Hakkında bu, Int? tanımının daha uzun sürümüdür. Swift Opsiyonel olarak, public enum Optional<Wrapped> : ExpressibleByNilLiteral

İlgili konular