In baseball, there are different types of players. There are contact hitters, power hitters, base stealers, power pitchers, junkballers, pinch hitters, and the guy on the bench who gives rookies hotfeet.
Contact hitters will hit at the top or bottom of the order. Power hitters will hit in the middle of the order. People who are not base stealers are not going to steal bases. Pinch hitters aren't going to play everyday and the guy on the bench isn't on the bench because he gives good hotfeet.
In other words, each of these types of players have specific roles.
Similarly, Swift has different data types that play specific roles. Today, we'll go over the string data type. In short, anything between two quotation marks is a string. Here are some examples:
"Ripken"
"The Giants win the pennant!"
"4256"
".406"
"%"
They are all strings because they are all surrounded by quotation marks. As you can see, a word can be a string. A sentence can be a string. A whole number or integer can be a string. A decimal or float can be a string. And a symbol can be a string. As long as whatever is written is between quotation marks, it is a string.
Sometimes you'll even see something like this:
var musialBattingAverage = ""
This is an example of an empty string. Why would someone write an empty string? Maybe there is a baseball app that requires the user to pick a player and a year before returning the player's batting average for that year. If, for instance, the user chooses Stan Musial and 1943, the value of "" will then change to ".357", Musial's batting average that year. If the user chooses Musial and his last year, 1963, it will change to ".255". musialBattingAverage is, after all, a variable and can change.
Why are strings important?
Strings help users of apps input information, edit information, and update information. If you are on baseball-reference.com, and you want to look up the career stats for Yogi Berra you can type in Yogi Berra in the search bar and baseball-reference.com will return Yogi's career stats for you. Here is how baseball-reference how they process these search queries:

In other words, if you tried to search using other data types such as integers (e.g., 20 (most strikeouts in a game), 287 (Hughie Jennings' record for most career HBPs), 1,406 (Rickey Henderson's record for stolen bases)), floats (.406 (Ted Williams' batting average in 1941), .215 (Mario Mendoza's lifetime batting average, believe it or not), 1.3791 (Babe Ruth's highest single season OPS)) or Booleans (values that are either true or false, e.g., Did Trevor Hoffman play for the Padres? True. Did Don Mattingly manage the Yankees? False.) the baseball-reference.com search engine would show you this:

What can you do with strings?
Good question! You can concatenate or add strings together like this:

When do you use strings?
I Googled "When do you use strings?" and I got nothing but advice on guitars. My ad hoc answer is we use strings to print out words and other values to the screen for users to view.
What can you not do with strings?
In the same way that a manager should not bat Mario Mendoza cleanup or ask 2012 Jamie Moyer to throw 98 MPH heat, there are things Swift strings cannot do.
As we saw above, you can add strings to other strings ("Derek" + "Jeter" = "Derek Jeter"), but you cannot add strings to other data types such as integers (the number 10), floats (.406), or Booleans (values that return either true or false and nothing else). If you try to add a string to another data type in Swift you'll get this error:

Rather, you'll need to cast or convert the 10 integer into a string. Here's what that looks like:

If you want clean this up a bit, you can add space between Rizzuto and 10 by doing this:

That's a lot of code for today so let's end with this:
On Deck: Cy Young, a Man of Many Integers
No comments:
Post a Comment