![]() ![]() This is because, again, Double has twice the accuracy of Float so it doesn't need to cut your number to fit. Now try changing the Float to be a Double and you'll see Swift prints the correct number out every time: var longitude: Double This is because it has limited space in which to store your number, so it's storing the most important part first – being off by 1,000,000 is a big thing, whereas being off by 0.000003 is less so. But if you look in the results pane you'll notice that as you add more numbers before the point, Swift is removing numbers after. That's adding increasing numbers before the decimal point, while keeping the same amount of numbers after. Try changing the code to be this: var longitude: Float Now, you might well say, "what does 0.000003 matter among friends?" but this is ably demonstrating what I was saying about accuracy.īecause these playgrounds update as you type, we can try things out so you can see exactly how Float and Double differ. We said that longitude should be equal to -86.783333, but in the results pane you'll see -86.78333 – it's missing one last 3 on the end. You can see both numbers appear on the right, but look carefully because there's a tiny discrepancy. Try putting this into your playground: var latitude: Double ![]() There are two data types for this because you get to choose how much accuracy you want, but most of the time it doesn't matter so the official Apple recommendation is always to use Double because it has the highest accuracy. This is Swift's way of storing numbers with a fractional component, such as 3.1, 3.141, 3.1415926, and -16777216.5. Let's look at two more data types, called Float and Double. ![]() You might think this is pedantic, but it's actually quite helpful: you make a promise that a variable will hold one particular type of data, and Xcode will enforce that throughout your work.īefore you go on, please delete those two lines of code causing the error, otherwise nothing in your playground will work going forward! Float and Double In that code, you're trying to put an integer into a string variable, and a string into an integer variable – and, thankfully, Xcode will throw up errors. Now that we have variables of two different types, you can see type safety in action. In this case, data types start with a capital letter, whereas variables and constants do not. A coding convention is something that doesn't matter to Swift (you can write your names how you like!) but does matter to other developers. Note how both String and Int have capital letters at the start, whereas name and age do not – this is the standard coding convention in Swift. That declares one variable to be a string and one to be an integer. We can try this out now by introducing another important data type, called Int, which is short for "integer." Integers are round numbers like 3, 30, 300, or -16777216. You can't escape it, and that's a good thing because it provides something called type safety – if you say "this will hold a string" then later try and put a rabbit in there, Swift will refuse. The lesson here is that Swift always wants to know what type of data every variable or constant will hold. Note: some people like to put a space before and after the colon, making var name : String, but they are wrong and you should try to avoid mentioning their wrongness in polite company. We know that name is going to be a string, so we can tell Swift that by writing a colon then String, like this: var name: String You've already seen how the first option looks, so let's look at the second: type annotations. ![]() If you were to rewrite your code to this it would stop working: var nameĪt this point you have two options: either create your variable and give it an initial value on one line of code, or use what's called a type annotation, which is where you tell Swift what data type the variable will hold later on, even though you aren't giving it a value right now. Swift knows that name should hold a string because you assign a string to it when you create it: "Tim McGraw". 10 letters) or even empty (no letters), it doesn't matter: they are all strings in Swift's eyes, and all work the same. You already saw one of the most important types when you assigned some text to a variable, but in Swift this is called a String – literally a string of characters. There are lots of kinds of data, and Swift handles them all individually. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |