0

I am having problems with calling the function in Swift, when building an iOS app.

@IBOutlet weak var vyseHypoteky: UITextField!
@IBOutlet weak var dobaSplaceni: UITextField!
@IBOutlet weak var urokovaSazba: UITextField!
@IBOutlet weak var mesicniSplatka: UITextField!

@IBAction func zmenaVyseHypoteky(sender: UISlider) {
    var currentValue = Int(sender.value)
    vyseHypoteky.text = "\(currentValue)"
    vypoctiSplatku()
}

@IBAction func zmenaDobySplaceni(sender: UISlider) {
    var currentValue = Int(sender.value)
    dobaSplaceni.text = "\(currentValue)"
}

@IBAction func zmenaUrokoveSazby(sender: UISlider) {
    var currentValue = Int(sender.value)
    urokovaSazba.text = "\(currentValue)"
}

func vypoctiSplatku () {
    let HU:Int? = vyseHypoteky.text.toInt()
    let ipa:Int? = urokovaSazba.text.toInt()
    let n:Int? = dobaSplaceni.text.toInt()

    var ipm = ipa! / 12

    var zavorka = 1+ipm
    var vypoctenaZavorka = mocnina(mocnenec: zavorka, mocnitel: n)

    var citatel = HU! * ipm * vypoctenaZavorka
    var jmenovatel = vypoctenaZavorka - 1

    var splatka = citatel / jmenovatel

    mesicniSplatka.text = ("\splatka")
    }

func mocnina (mocnenec: Int, mocnitel: Int) -> Int {
    var mocnina = 1
    for _ in 1...mocnitel {
        mocnina *= mocnenec
    }
    return mocnina
    }

The app is calculating a number by my formula. I want to use my function to calculate the x^y, this the "mocnina" function where I want to use two int, the x is "mocnenec" and the y is "mocnitel".

And finally I want to send the final int from variable "splatka" to text inout filed "mesicniSplatka".

But I am getting errors in calling the function "mocnina" --> var vypoctenaZavorka = mocnina(mocnenec: zavorka, mocnitel: n)

Extraneous argument label 'mocnenec:' in call Value of optional type 'Int?' not unwrapped; did you mean to use '!' or '?'?

and in the end with mesicniSplatka.text = ("\splatka")

Invalid escape sequence in literal

How to fix it? Thx for helping a total newbie :)

  • 1
    Note that using english names for variables, functions, etc... would make it much easier for the majority of people to understand the purpose of your code. – Martin R Jan 04 '15 at 18:06
  • Well, the last one is easy `("\splatka")` should be `"\(splatka)"`. (I suggest reading the free Swift book and trying the exercises.) – Phillip Mills Jan 04 '15 at 18:08
  • Other commentators have answered the first & third of your questions - the middle one about 'extraneous labels' is because your functions are functions, not class methods, and so calling them with named parameters needs an external name as well as an internal name. See here https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html – Grimxn Jan 04 '15 at 18:19

2 Answers2

0

In your code, n is declared of type Int?, it means "Optional Int". This is normal because the toInt() function is not guaranteed to succeed, and might return nil if the text is not convertible to an integer. So, you need to unwrap it first, like this: var vypoctenaZavorka = mocnina(mocnenec: zavorka, mocnitel: n!). Or, if you're not sure the conversion from string succeeded, do something like this:

let HU:Int? = vyseHypoteky.text.toInt()
let ipa:Int? = urokovaSazba.text.toInt()
let n:Int? = dobaSplaceni.text.toInt()

if ipa != nil {
    var ipm = ipa! / 12

    var zavorka = 1+ipm
    if n != nil {
        var vypoctenaZavorka = mocnina(mocnenec: zavorka, mocnitel: n)

        // etc...

    }
}
Romain
  • 3,718
  • 3
  • 32
  • 48
0

The problem is that n is a wrapped:

var n:Int?

So you have a few options, you can either change your code to unwrap it when you use it (probably a good idea to check for nil before doing this as this will cause an exception if n is nil):

var vypoctenaZavorka = mocnina(mocnenec: zavorka, mocnitel: n!)

Or you can unwrap it when you create it:

let n:Int = dobaSplaceni.text.toInt()!

If you'd like to better understand "when to use optionals", I've written a very long explanation to this question on the subject: UIViewController variables initialization

Community
  • 1
  • 1
Oxcug
  • 6,524
  • 2
  • 31
  • 46