I am trying to make a monthly Loan payment calculator
here is the code for it:
import UIKit
class monthlypayment: UIViewController {
#IBOutlet weak var loanamounttextfield: UITextField!
#IBOutlet weak var numberofmonthstextfield: UITextField!
#IBOutlet weak var loanpercentagetextfield: UITextField!
#IBOutlet weak var answerlabel: UILabel!
#IBAction func calculate(sender: AnyObject) {
var loanamounttext = loanamounttextfield.text
var loanamount:Double! = Double((loanamounttext as NSString).doubleValue)
var loanpercentage = loanpercentagetextfield.text
var loanpercentagedouble:Double! = Double((loanpercentage as NSString).doubleValue)
var numbrofmonths = numberofmonthstextfield.text
var numberofmonths:Double! = Double((numbrofmonths as NSString).doubleValue)
var rate = loanpercentagedouble/1200
var monthlypayment:Double = rate+(rate/(1.0+rate)^numberofmonths)-1.0*loanamount
}
}
I get the error here
var rate = loanpercentagedouble/1200
var monthlypayment:Double = rate+ (rate/(1.0+rate)^numberofmonths)-1.0*loanamount
Where numberofmonths and loanamount are doubles as you can see
But I am getting the error:
Could not find '+' that accepts the supplied arguments
In Swift ^ is the XOR operator (which return various flavours of Ints) not the exponential, you want to use the pow(num, power) function instead
var monthlypayment = rate + pow(rate / (1.0 + rate), numberofmonths) - 1.0 * loan amount
Alternatively you can define a custom operator
infix operator ** { associativity left precedence 160 }
func ** (left: Double, right: Double) -> Double {
return pow(left, right)
}
And rewrite your code to a more readable
var monthlypayment = rate + (rate / (1.0 + rate)) ** numberofmonths - 1.0 * loanamount
Related
I am a beginner with Xcode and Swift. Below is my formula for the mortgage calculator. It is working correct in Playground but when I transfer it to the ViewController tab that is when Xcode is giving me a bunch of errors. Can someone please help?
let r: Double = interestRate / 1200
let n: Double = years * 12
let p: Double = pow(1 + r, n)
let monthPay = loan * r * p / (p - 1)
print(monthPay)
So in my View Controller
valueA is Loan Amount
valueB is Number of Payments
valueC is Interest Rate
underneath that you will have a calculate button that will print the results underneath in a label currently named results. It may be that when i rename everything to the values is when the issues occur.
ViewController Code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var valueA: UITextField!
#IBOutlet weak var valueB: UITextField!
#IBOutlet weak var valueC: UITextField!
#IBOutlet weak var results: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button(_ sender: Any) {
let a = Int(valueA.text!)
let b = Int(valueB.text!)
let c = Int(valueC.text!)
let answer = a! * c! * pow(1 + c!, b!) / (pow(1 + c!, b!))
results.text = "$\(answer)"
}
}
Your issue is that you are converting your textfield values to integers and trying to multiply them by the result of pow's method which returns a Double. Note that if the user enters an invalid value your app will crash if you force unwrap the result. Yo can use nil coalescing operator ?? to provide a default value .zero in case of failure. Try like this:
class ViewController: UIViewController {
#IBOutlet weak var valueA: UITextField! // loan amount
#IBOutlet weak var valueB: UITextField! // Number of Payments
#IBOutlet weak var valueC: UITextField! // Interest Rate
#IBOutlet weak var results: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button(_ sender: Any) {
let loanAmount = Double(valueA.text!) ?? .zero
let numberOfPayments = Double(valueB.text!) ?? .zero
let interestRate = Double(valueC.text!) ?? .zero
let rate = interestRate / 100 / 12
let answer = loanAmount * rate / (1 - pow(1 + rate, -numberOfPayments))
results.text = Formatter.currency.string(for: answer)
}
}
extension Formatter {
static let currency: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = .init(identifier: "en_US_POSIX")
formatter.numberStyle = .currency
return formatter
}()
}
var widthValue : Int = 0
var heightValue : Int = 0
var result : Int = 0
#IBOutlet weak var widthText: UITextField!
#IBOutlet weak var heightText: UITextField!
#IBOutlet weak var resultText: UILabel!
#IBAction func calculate(_ sender: Any) {
widthText.text = "\(widthValue)"
heightText.text = "\(heightValue)"
resultText.text = "\(widthValue + heightValue)"
}
Two UITextFields to hold an Int then resultText displays in a UILabel named resultText.
I figure you're trying to add numbers from two text fields.
For that,
Change following
resultText.text = "\(widthValue + heightValue)"
To
let additionResult = widthText.text + heightText.text
resultText.text = additionResult
You should be getting the values of widthText and heightText. Something like this would work
#IBAction func calculate(_ sender: UIView) {
resultText.text = String(Int(heightText.text!)! + Int(widthText.text!)!)
}
Just starting learning swift but stuck when trying to multiply an input with another number and display on a label. I get the error that the number isn't a string and tried to cast but didn't work.
class ViewController: UIViewController {
#IBOutlet weak var entry: UITextField!
#IBOutlet weak var answer: UILabel!
#IBAction func button(_ sender: Any) {
answer.text = entry.text * 2
}
}
You should cast the text into a Double, an Int etc., then convert the calculation to a string.
if let entry = Double(entry.text) {
answer.text = "\(entry * 2)"
}
or
if let entry = Int(entry.text) {
answer.text = "\(entry * 2)"
}
If you know that the entry will hold a number
answer.text = String(Int(entry.text)! * 2)
Using optional unwrapping instead
if let num = Int(entry.text) {
answer.text = String(num * 2)
}
So here is my if statement:
if !DAGNViewControllerInstance.DValueVar<=5 || !DAGNViewControllerInstance.DValueVar>=1 || !DAGNViewControllerInstance.AValueVar<=6 || !DAGNViewControllerInstance.AValueVar>=2 || !DAGNViewControllerInstance.GValueVar<=3 || !DAGNViewControllerInstance.GValueVar>=2 || !DAGNViewControllerInstance.NValueVar<=3 || !DAGNViewControllerInstance.NValueVar>=1 || !DAGNViewControllerInstance.TValueVar<=9999 || !DAGNViewControllerInstance.TValueVar>=1 {
self.ErrorField.text = "One or more values are not in range!"
}
This if statement checks a variable to see if it is in range or not. But whenever I try to build the code I get an error from this if statement.
Swift Compiler Error Cannot Invoke '||' with an argument list of type '($T88, $T96)'
The ValueVars are floats. The DAGNViewControllerInstance is a instance of this UIViewController subclass:
import UIKit
extension String {
var floatValue: Float {
return (self as NSString).floatValue
}
}
class DAGNViewController: UIViewController {
#IBOutlet weak var DValue: UITextField!
#IBOutlet weak var AValue: UITextField!
#IBOutlet weak var GValue: UITextField!
#IBOutlet weak var NValue: UITextField!
#IBOutlet weak var TValue: UITextField!
var DValueVar: Float = Float()
var AValueVar: Float = Float()
var GValueVar: Float = Float()
var NValueVar: Float = Float()
var TValueVar: Float = Float()
#IBAction func GOButtonPressed(sender: UIButton) {
DValueVar = DValue.text.floatValue
AValueVar = AValue.text.floatValue
GValueVar = GValue.text.floatValue
NValueVar = NValue.text.floatValue
TValueVar = NValue.text.floatValue
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The Value IBOutlets are linked to UITextFields. The if statement is in another UIViewController subclass which is the custom class of a UIView in the storyboard which has a label that I want to display the error message.
Does anyone know why I get the error, and or how to fix it?
The problem is probably with the order of operator application, combined with the ambiguity of the numeric literals (which could be floats, doubles, integers etc). For example, in a simpler case:
let d = 12.3
let e = 45.6
// error: cannot invoke '>=' with an argument list of type '($T6, $T12)'
if !d>=3 || !e>=4 { }
// compiles without error
if !(d>3) || !(e>=4) { }
Try applying parenthesis to the various expressions and that should resolve it.
P.S. you may want to look at using intervals to simplify this:
let d = 6.5
if !(1...5).contains(d) {
println("d not within range")
}
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 :)
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
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...
}
}