NMaks
NMaks

Reputation: 249

I get optional value where I mustn't. swift 3.2

I was migrating from swift 2.2 to swift 3.2

and some strange behavior occurred, I got optional value where I mustn't.

my code:

if let tmpStr = strFromFld?.replacingOccurrences(of: sess.decimalPoint, with: ".") {
            if convType == "buy" {
                print("Why optional here")
                print(tmpStr) //this shows tmpStr is optional 
                print("???")
}

link to picture where Xcode shows tmpStr as optional

full function, sorry for bad coding, don't laugh ):

func updateInfoForSum(_ strFromFld:String?) {
        guard strFromFld != nil else {return}
        if let tmpStr = strFromFld?.replacingOccurrences(of: sess.decimalPoint, with: ".") {
            if convType == "buy" {
                print("Why optional here")
                print(tmpStr)
                print("???")

                infoSumConvert.text = "Сумма " +
                    WalletStuff.doubleToMoneyStr(
                        Double(
                            truncAfter2SymbolAfterDecimalDelimeter(
                                String(
                                    NSString(string: tmpStr).doubleValue / selectedCurrency.saleRate!
                                    ).replacingOccurrences(of: ".", with: sess.decimalPoint)
                                ).replacingOccurrences(of: sess.decimalPoint, with: ".")
                            )!
                    )
                    + " \(selectedCurrency.name)"
            } else {
                infoSumConvert.text = "Сумма " +
                    WalletStuff.doubleToMoneyStr(
                        Double(
                            truncAfter2SymbolAfterDecimalDelimeter(
                                String(
                                    NSString(string: tmpStr).doubleValue * selectedCurrency.buyRate!
                                    ).replacingOccurrences(of: ".", with: sess.decimalPoint)
                                ).replacingOccurrences(of: sess.decimalPoint, with: ".")
                            )!
                    )
                    + " UZS"
            }
        }

    }

Migrating to swift 4.1 does not help neither ((

Upvotes: 0

Views: 101

Answers (1)

matt
matt

Reputation: 536027

Your premise is wrong. tmpStr is not an Optional String. It is a normal string whose text contains the word “Optional”. The string itself says "Optional(1000)" but it is a normal String.

Upvotes: 1

Related Questions