1

I wanted to create a function that would get the current Date, divided it in components(.year, .month, .day) to concatenate it inside a var integrationKey. The problem is, whenever I call print (chave()) I get this sL8xlbkw2Optional(364)kLx3i832190461000160Lxd5yV0Optional(45)sKc3gHx9Optional(347)printed on the log.

Here is what the code looks like:

class ViewController: UIViewController {
    var dia: Int!
    var mes: Int!
    var ano: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
     print (chave())
    }

    override func didReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func chave () -> (String){

        //Pega data atual
        let currentDate = Date()

        //Pega calendario do usuarios
        let userCalendar = Calendar.current

        //Array de compnentes necessarios
        let requestedComponents: Set<Calendar.Component> = [
        .year,
        .month,
        .day
        ]

        //Pega os componentes
        let dateComponents = userCalendar.dateComponents(requestedComponents, from: currentDate)

        //componentes disponiveis

        dia = dateComponents.day! + 37
        mes = dateComponents.month! + 335
        ano = dateComponents.year! - 1652



        var chaveDeIntegracao = "sL8xlbkw2\(ano)kLx3i832190461000160Lxd5yV0\(dia)sKc3gHx9\(mes)"

        //return chaveDeIntegracao
        return chaveDeIntegracao
    }




    func chave () -> (String){

            //Pega data atual
            let currentDate = Date()

            //Pega calendario do usuarios
            let userCalendar = Calendar.current

            //Array de compnentes necessarios
            let requestedComponents: Set<Calendar.Component> = [
            .year,
            .month,
            .day
            ]

            //Pega os componentes
            let dateComponents = userCalendar.dateComponents(requestedComponents, from: currentDate)

            //componentes disponiveis

            dia = dateComponents.day! + 37
            mes = dateComponents.month! + 335
            ano = dateComponents.year! - 1652



            var chaveDeIntegracao = "sL8xlbkw2\(ano)kLx3i832190461000160Lxd5yV0\(dia)sKc3gHx9\(mes)"

            //return chaveDeIntegracao
            return chaveDeIntegracao
        }

And here is what was printed on the console: sL8xlbkw2Optional(364)kLx3i832190461000160Lxd5yV0Optional(45)sKc3gHx9O

The question is, will these Optionals appear whenever i call this function and what about the () surrounding the values?

Joe Sene
  • 139
  • 1
  • 12

0 Answers0