Vai al contenuto

[Risolto]Aiuto Con Xcode 8 Swift 3


Messaggi raccomandati

Ciao a tutti 

come da titolo ho un problema con Swift 3

Sto provando a realizzare un app che,praticamente tramite un dataPicker scelgo una data e in una Label dovrebbe apparire cosi:  "10 mesi 23 giorni"

Il problema è che nella Label appare cosi :" Optional(10) mesi, Optional(23) giorni"

Non mi da nessun errore ne warning, dove sbaglio?

 

questo il codice:

 

 OperatorPicker.addTarget(self, action: #selector(ViewController.datePickerChanged(_OperatorPicker:)),

                                 for: UIControlEvents.valueChanged)

        

        totalLabel.alpha = 0

        setupDatePicker()

        

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    func datePickerChanged(_OperatorPicker:UIDatePicker) {

        let currentDate = Date()

        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "EEEE,MMM dd,yyyy"

        dateFormatter.locale = Locale(identifier: "it_IT")

        dateFormatter.locale = Locale.current

        dateFormatter.dateStyle = DateFormatter.Style.long

        

        let myDate = dateFormatter.string(from: OperatorPicker.date)

        myLabel.text = myDate

        

        

        let dateComponentsFormatter = DateComponentsFormatter()

        

        dateComponentsFormatter.unitsStyle = DateComponentsFormatter.UnitsStyle.full

        let numberDay = 0

        

        

        dateComponentsFormatter.allowedUnits = [NSCalendar.Unit.day]

        _ = dateComponentsFormatter.string(from: currentDate, to: OperatorPicker.date)

        

        var newDateComponents = DateComponents()

        newDateComponents.day = numberDay

        

        

        let diffDate = (Calendar.current as NSCalendar).components([NSCalendar.Unit.month, NSCalendar.Unit.day], from: currentDate, to: OperatorPicker.date, options: NSCalendar.Options.init(rawValue: 0))

        

        dateFormatter.dateFormat = "MM,dddd"

        let rawDate = dateFormatter.string(from: currentDate)

        totalLabel.text = rawDate.capitalized

        totalLabel.text = ("\(diffDate.month) mesi, \(diffDate.day) giorni")

        

        totalLabel.alpha = 1

    }

    func setupDatePicker() {

        OperatorPicker.setValue(UIColor.blue, forKey: "textColor")

        OperatorPicker.backgroundColor = UIColor.clear

    }

    

    }

 

Link al commento
Condividi su altri siti

Archiviato

Questa discussione è archiviata e chiusa a future risposte.

×
×
  • Crea Nuovo...