字符串连接和可选(值)打印

时间:2016-11-08 11:58:53

标签: ios swift string-concatenation

我想创建一个获取当前Date的函数,将其除以组件(.year,.month,.day)以在var integrationKey内连接它。 问题是,每当我拨打print (chave())时,我都会在日志上打印sL8xlbkw2Optional(364)kLx3i832190461000160Lxd5yV0Optional(45)sKc3gHx9Optional(347)

代码如下所示:

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
        }

以下是控制台上打印的内容: sL8xlbkw2Optional(364)kLx3i832190461000160Lxd5yV0Optional(45)sKc3gHx9O

问题是,每当我调用这个函数时,这些Optionals会出现吗?值周围的值是什么?

0 个答案:

没有答案