how fix roundig operation in swift - calculator

I'm new to swift.
I usually use other languages, I made a calculator but I have a problem with some operations, for example when I print (1666.66 * 10) it prints 16666.60 ... 02 can you tell me why and how to fix it?
thanks

Related

swift programming language

I'm trying to learn a new coding language called swift. But I got a question in mind.
import UIkit
func makeACake (cash:Double, ratio:Double){
print(Making \(cash*ratio)pounds of cake")
}
makeACake(cash:10,ratio:3)
// it will print making 30.0 pounds of cake.
just two questions here.
no.1
why cant i just call makeACake(10,3) why do i have to type it in this manner makeACake(cash:10,ratio:3)
no.2
is the function println removed from swift? why cant i use the function println and it asked me to use print instead.
sorry if i happened to ask any stupid question. but yea just trying to learn coding so maybe help a newbie out? thank you and bless the person who would be kind enough to help me and let me move on.
Every programing language has their own syntax.
Basically, this syntax is acquired from their parent language. The syntax is getting changed in every update.
I have started with swift2 and lot has been changed from swift2 to swift4.
So answering your question,
Answer 1: (_) Underscore --> It is Wildcard pattern
A wildcard pattern matches and ignores any value and consists of an
underscore (_). Use a wildcard pattern when you don’t care about the
values being matched against.
In your example, It means that argument labels are not necessary on invocation of your function.
Learn more about patter at: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html
So if you dont want to write parameter name write:
func makeACake ( _ cash:Double, _ ratio:Double){}
Answer 2: print: Introduce First in swift2, we used to type println in swift.
Now you might ask why println was removed, and the answer is simple "It is no longer required".
Let me take a simple example:
println("Hello")
println("World")
output: (One next line is extra in output, don't know how to format it here)
Hello
World
While with print
print("Hello")
print("World")
output: HelloWorld
Later in swift2 println was deprecated and the same("Next Line") output can be achieved with print itself.
"Evolution"
They are making swift "Easy to Learn", "Easy to code" language

What are your thinking about Yoda conditions in Swift IOS [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I just to know from experienced IOS Developers, what are the advantages and disadvantages of using the "yoda condition". As I am learning the swift language. I don't know much about this topic. I did not find any suitable answer. Any help would Highly be appreciated. Thank you.
C, C++, Objective-C, and Java (and some other languages) have three properties that work together in an error-prone way:
Assignment is an expression. That is, x = 1 is an expression with a value (the value 1 in this example).
Conditionals in control flow statements (like if and while statements) allow any expression that can be coerced to a Boolean value. Integers, floating point numbers, and pointers can all be coerced to Boolean: zero or null means false and anything else means true.
The assignment operator = and the equality operator == are very similar.
Because of these three features, it is easy to accidentally write if (x = 1) when you meant to write if (x == 1). Your program still compiles, but probably behaves incorrectly some of the time.
The “Yoda conditional” is a style that helps prevent this error. If you are in the habit of writing if (1 == x), and you accidentally write if (1 = x), your program will not compile.
However, the if (x = 1) error cannot happen in Swift, because Swift lacks two of the three properties described above.
In Swift, an assignment like x = 1 is a statement, not an expression. It cannot be used as the conditional of a control statement.
In Swift, the conditional of a control statement must explicitly be a Bool. No other types are automatically coerced to Bool. Even if assigment were an expression, if (x = 1) would still be prohibited because x = 1 would have the type of x, which cannot be Bool because 1 is not a Bool value.
So there is no reason to ever use a Yoda condition in Swift (unless you find it clearer for some other reason).
Note also that many modern compilers for other languages will print a warning if you use an assignment as a conditional, so Yoda syntax isn't as useful as it used to be in C, C++, and Objective-C.
The concept of a "Yoda condition" is nonsensical, because:
the putative idea of "Yoda" style is: the two sides of the "==" operator, are reversed "from what the usually would be"...
However, that is nonsensical.
Because
there is no sense at all of what they "usually would be".
It's a fairly dumb piece of "made-up" lingo.
Programmers write the if condition,
simply depending on,
what feels best to express the algorithm at hand.
Forget about it.
At best, it is of historic interest as a curiosity, irrelevant to anyone young enough to be alive today. To any new programmers learning (such as the OP here), it's irrelevant.
In the sense of the OP's question, it is irrelevant: there is no right or wrong order.
At best it's a matter of style to express the idea of the algorithm at hand.
Just to expand on what others have already said, yoda conditions are a holdover from C because of a specific bug that is easy to make in C and impossible in Swift. The bug in question goes like this:
if (x = 5) { ... }
The problem is that x = 5 is an assignment. You probably meant x == 5. In C, assignment statements return the value, so x = 5 returns 5, which is not 0, so it's true no matter what x is. This was a really common bug until compilers finally started to warn you about it. C actually relies on this, so it's not something C could remove. It's very common to use this in code like:
if (ch = getchar()) { ... }
The fix to this bug was to invert the condition by habit. if (5 = x) { ... } is a syntax error because 5 is a literal.
This is all irrelevant in Swift. Swift made = a statement rather than an expression, specifically to avoid this kind of bug. So if x = 5 { ...} is a syntax error already.
Yoda style just makes things harder to read; don't use it in Swift. Even in C, the compiler will give you stern warnings about this bug, so unless you are working in an old code base where it is the style, there's no reason to use it even there.
I'm not an experienced PHP programmer, where I believe it is also common. There it may still make sense. But not in Swift.

Initialization of immutable value error on Swift?

I am trying to create an app where you can chose the type of chemistry you are interested in and it will give you information on that type. The only problem is that I am getting a error message stating the following:
Initialization of immature value 'chemistry' was never used.
On this block of code:
override func viewDidLoad() {
super.viewDidLoad()
chemistryArray = [chemistryLabel0, chemistryLabel1, chemistryLabel2, chemistryLabel3, chemistryLabel4]
for index in 0..<chemistryArray.count {
let chemistry = chemistryArray[index]
}
}
On this line:
let chemistry = chemistryArray[index]
I was wondering what this error message means and how to fix it.
Any suggestions or input is grealty appreciated
Thanks in advance
I think every other loop you declare chemistry again and never use it inside the loop. And as I was corrected this variable is not seen outside the scope of the for-loop.
So the purpose of the for-loop is ambiguous.
Sorry, I would rather comment but I cannot yet.
As Martin R already explained, you are declaring and populating the constant chemistry but you are not using it.
Since chemistry is declared within the { } of the for loop, it does exist only inside that scope.
So the compiler, looking at your code, cannot figure out (me neither :) the meaning of your code and it is producing a warning to grab your attention.
In other words the compiler is telling you something like:
Dear coder,
I will compile this code but are you sure it is correct? Because this instruction does not make sense to me.
Regards
The compiler
The fixing
To fix the warning you just need to use the constant or remove it.
Now my question
Why are you declaring and populating a constant and you are not using it?

book example not working in SWIFT Playground

I'm looking for an explanation on playground behavior for Swift. On page 76 of the book Beginning Swift Programming the doSomething function doesn't behave in Xcode as described.
func doSomething(num1: Int, num2: Int) {
println(num1, num2)
}
doSomething(5,6)
The book doesn't show an answer, but I expect a response like (5,6). However, I get no error nor any response. Change the action to println(num1) and doSomething(5,6) works. It produces 5. So does doSomething(5). For that matter. Change it to println((num1, num2)) and doSomething(5,6) yields (5,6).
I'm using Xcode v.6.4 on a Mac running Yosemite. What's going on?
As far as I know, the println() function takes only one parameter.
You either do:
println((num1, num2)) // for printing as a Tuple object
or:
println("\(num1), \(num2)") // for printing as a String object
Reference: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309
When in a normal project rather than Playground, the code you provided actually works. However, you will have to call doSomething(5, num2: 6) instead. (Swift 1.2/2.0)
It's Swift basic knowledge and suggest you to look up answer rather than ask here. If you want to print something using println with variable. You have to use \(variableName). For example:
println("\(num1), \(num2)")

Variable increment in for loop doesn't work

I have this Swift code:
for var a = 0; a < 10; a++{
println(a)
}
There is an compile error on
a++{
Can anyone explain why?
You just need to add a space between a++ and {:
for var a = 0; a < 10; a++ {
println(a)
}
If you want to use the "{" against your variable you need to use the variable name between the "+" and the "{" as per the swift documentation
for var a = 0; a < 10; ++a{
println(a)
}
Another option as suggest ABakerSmith is to space the operators "+" and "{"
I particularly prefer the first option as it keeps my code consistent as I never use space before my "{" and also it is how is used through all apple documentation
#vacawama and ABakerSmith already told you how to fix it. The reason is that Swift uses whitespace to figure out the difference between multi-character expressions and separate expressions. It requires whitespace between symbols where languages like C don't. It still trips me up sometimes.
Also, for future reference, Swift code allows for two different For loop syntaxes.
for <initialization>; <condition>; <increment> { <statements> }
or when in an array or a collection
for <identifier> in <collection> { <statements> }
But both of them require the attention to detail on where your spaces are in the code, so be careful.
Also, since it seems like you may be rather new at Swift, I recommend checking out these awesome resources that make the journey of learning Swift a lot easier.
Apple's free 500 page Swift Code Reference Guide
Thinkster.io has a great guide for everything swift, even quick little cheat sheets to keep handy for any questions you might have in the future. When I learned swift I used this site a lot!
If you want to build a cool little game using swift start here!
Hope that helped! Swift is a great programming language that has a lot to offer, and I hope you have fun learning it!