How To Change The Data Type In Swift
Types of Information
There are lots of kinds of data, and Swift handles them all individually. You already saw one of the nearly important types when you assigned some text to a variable, merely in Swift this is called a String
– literally a string of characters.
Strings can be long (e.g. a one thousand thousand letters or more), brusk (e.g. 10 messages) or fifty-fifty empty (no messages), information technology doesn't thing: they are all strings in Swift's eyes, and all work the same. Swift knows that name
should hold a cord considering you assign a string to it when y'all create it: "Tim McGraw". If you were to rewrite your code to this it would stop working:
var name proper name = "Tim McGraw"
This fourth dimension Xcode will give you an error bulletin that won't make much sense merely yet: "Blazon note missing in pattern". What it means is, "I tin't figure out what data type name
is because you aren't giving me plenty information."
At this indicate you take two options: either create your variable and requite it an initial value on one line of code, or employ what'due south called a blazon annotation, which is where you lot tell Swift what information type the variable will hold later on, even though y'all aren't giving information technology a value right at present.
You've already seen how the first option looks, and so let's look at the second: blazon annotations. We know that name
is going to be a string, so we can tell Swift that by writing a colon then String
, similar this:
var name: String proper name = "Tim McGraw"
Annotation: some people like to put a space before and later on the colon, making var proper noun : String
, but they are wrong and y'all should try to avert mentioning their wrongness in polite company.
The lesson here is that Swift always wants to know what type of data every variable or abiding will hold. Always. You lot tin can't escape it, and that'due south a skillful thing because it provides something chosen blazon safety – if you say "this volition concur a cord" and then afterwards effort and put a rabbit in there, Swift will pass up.
We tin can endeavour this out now by introducing another of import data type, called Int, which is short for "integer." Integers are round numbers like 3, 30, 300, or -16777216. For instance:
var name: String name = "Tim McGraw" var age: Int age = 25
That declares one variable to exist a string and 1 to be an integer. Notation how both String
and Int
have capital letters at the get-go, whereas proper name
and age
do not – this is the standard coding convention in Swift. A coding convention is something that doesn't matter to Swift (y'all can write your names how you like!) only does matter to other developers. In this case, data types start with a capital letter letter, whereas variables and constants do non.
Now that we have variables of two different types, yous can meet type condom in action. Effort writing this:
proper noun = 25 age = "Tim McGraw"
In that code, you lot're trying to put an integer into a string variable, and a string into an integer variable – and, thankfully, Xcode will throw up errors. You might think this is pedantic, just it's actually quite helpful: you make a promise that a variable will hold i item blazon of information, and Xcode will enforce that throughout your work.
Before you go on, please delete those 2 lines of code causing the error, otherwise cipher in your playground will work going forward!
Bladder and Double
Let's look at 2 more data types, called Bladder
and Double
. This is Swift'south mode of storing numbers with a fractional component, such as 3.1, 3.141, iii.1415926, and -16777216.5. There are two information types for this because y'all get to choose how much accurateness you want, but most of the time information technology doesn't matter so the official Apple tree recommendation is always to use Double
because it has the highest accuracy.
Try putting this into your playground:
var breadth: Double latitude = 36.166667 var longitude: Bladder longitude = -86.783333
You can run across both numbers appear on the correct, but look advisedly considering at that place'south a tiny discrepancy. We said that longitude
should exist equal to -86.783333, but in the results pane you'll see -86.78333 – information technology's missing one terminal three on the end. Now, you lot might well say, "what does 0.000003 matter among friends?" simply this is ably demonstrating what I was maxim almost accuracy.
Considering these playgrounds update every bit you type, we can endeavour things out so you can see exactly how Bladder
and Double
differ. Try changing the lawmaking to be this:
var longitude: Float longitude = -86.783333 longitude = -186.783333 longitude = -1286.783333 longitude = -12386.783333 longitude = -123486.783333 longitude = -1234586.783333
That's adding increasing numbers before the decimal indicate, while keeping the aforementioned amount of numbers after. Just if you lot look in the results pane you'll notice that as you add together more than numbers before the indicate, Swift is removing numbers later on. This is considering it has limited space in which to store your number, and so information technology's storing the most important part beginning – being off past i,000,000 is a big thing, whereas being off by 0.000003 is less so.
Now endeavour irresolute the Float
to exist a Double
and you'll encounter Swift prints the correct number out every time:
var longitude: Double
This is considering, again, Double
has twice the accuracy of Float
so it doesn't demand to cut your number to fit. Doubles still have limits, though – if you were to endeavour a massive number like 123456789.123456789 you would see information technology gets cutting downward to 123456789.1234568.
Boolean
Swift has a built-in data blazon that can shop whether a value is true or false, called a Bool
, brusk for Boolean. Bools don't have space for "maybe" or "perhaps", only absolutes: truthful or fake. For case:
var stayOutTooLate: Bool stayOutTooLate = true var nothingInBrain: Bool nothingInBrain = true var missABeat: Bool missABeat = false
Tip: You'll notice these variables are written in a very particular way: we don't write MissABeat
, missabeat
, or other such variations, but instead make the initial letter lowercase and so capitalize the first letter of the second and subsequent words. This is called "camel case" because it looks a bit like the humps of a camel, and it'southward used to make information technology easier to read words in variable names.
Using type annotations wisely
As you've learned, there are two ways to tell Swift what blazon of data a variable holds: assign a value when you create the variable, or use a type annotation. If you have a choice, the starting time is ever preferable because it's clearer. For example:
var proper name = "Tim McGraw"
…is preferred to:
var name: String proper noun = "Tim McGraw"
This applies to all data types. For example:
var age = 25 var longitude = -86.783333 var nothingInBrain = true
This technique is called blazon inference, because Swift can infer what data type should exist used for a variable by looking at the type of data you want to put in there. When it comes to numbers like -86.783333, Swift will always infer a Double
rather than a Float
.
For the sake of completeness, I should add that it's possible to specify a data type and provide a value at the same fourth dimension, like this:
var name: Cord = "Tim McGraw"
Sponsor Hacking with Swift and accomplish the earth's largest Swift community!
Source: https://www.hackingwithswift.com/read/0/3/types-of-data
Posted by: rodriguezfloory38.blogspot.com
0 Response to "How To Change The Data Type In Swift"
Post a Comment