Menu

Day 9 - Sunny and hot

Sep 08, 2015 at 08:52 am, by Pierre Liard

To understand how Rob is able to take particular data from the weather-forecast website, we have to look at strings manipulation. The class String or NSString (explained soon) offers several possibilities for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks. The simplest manipulation is of course to add or, in in its technical expression, concatenate a string to another:

var string = "Hello"
var newString = string + " Susan" //displays: Hello Susan

It is also possible to select some parts of a string, because each character has a precise position determined by an index starting at zero (like arrays). For example, the position of "o" in the string "Hello" is at index 4. Strings need to be converted to the class NSString in order to add more functionalities. String and NSString are mostly similar, but some methods working with NSString do not run with string. It may change in the future, but it seems better for now to use NSString when in need of a larger range of methods. By doing this conversion, it is now possible to display our string up to or from a certain index, or, even better, show a range of characters, like I have done here in Playground:

string manipulation 1

There are several other methods manipulating strings, but I will focus now on componentsSeparatedByString(). As its name implies, it allows to divide a string into substrings and to specify the character(s) that is (are) separating them. It returns an array that, as we know, is indexable. An example will make this explanation clearer:

string manipulation 2

"Sunny" acts as a divider: it separates the string into two distinct values with each a key (or index). In this example, index[0] and index[1] are separated by ", ",. It is then possible to extract the desired data. This process is however pretty fragile because, if something is modified afterwards in the string's order, the code won't run anymore. So, as underlined yesterday, this extraction information method shouldn't be used in a production app because of the high risk of crash involved.

To display information in the relatively small display of an iPhone, the strings have to be short. It's for this reason that Rob takes only the string containing the three days weather forecast summary. It is possible to find this specific section by simply opening the View Page Source in your browser and to conduct a search by typing "Summary" for instance:

string manipulation 3

The section highlighted in blue is used as the method's argument, or in other words, as the delimitation criterium. The code itself creates an array of two indexes by separating the string with the argument inside the method's parentheses (see above picture):

string manipulation 4

Notice the backslashes before and after the quotation marks inside the string: if they were not present, Swift would consider the first quotation mark it will encounter as the end of the string (in this string it will the quotation mark in front of read-more-small), and returns an unexpected result. By adding them to each quotation mark within the string, Swift will overlook them until there is a non escaped sequence or an absence of backslash. By selecting the second index [1], only the portion of the string after the delimitation will be shown. In the case of the weather-forecast website, the index[1] is still much too long and contains useless information for our purpose. By repeating the same process but with another delimitation, it is then possible to select exactly and only the section related to three days weather forecast. To make more understandable the whole process, I have modified my previous Playground example by selecting only the weather in Paris as relevant information:

string manipulation final

That's almost it! There are still two difficulties to be solved before having a functional app: what to do with cities composed of two words, like San Francisco or Los Angeles, and how to modify some html characters reported in the result label? If a two words city is typed in the app's textfield, it will generally crash because Swift doesn't know what to do with two cities names in the same field. To avoid that, a new string manipulation is necessary to substitute the white space with something else. When typing for instance San Francisco in our weather-forecast website, the white space is automatically replaced by an hyphen in the URL. By doing the same in Swift, it will avoid the app a fatal error. A method called stringByReplacingOccurrencesOfString() has for function to do just that:

string substitution

Our second problem is now just a breeze to solve: when displaying the temperatures into the app, html characters are shown:

degree and html encoding 1

The ampersand deg; is html encoding for the character º (on Mac, type option + zero) representing a degree. To make it appear as such , a character substitution is not surprisingly used in the exact same way as for the white space:

degree and html encoding 2

That's all for this chapter 5: I haven't talked about every single detail, but the least I can say is that I started diving pretty deeply into Swift 2. It may be worth to remember what has been covered: I began with timer and a stopwatch app, I then moved to Tables and learned how to add data to a table view by building a times table app (not demonstrated), next I saw how to store data permanently within the app and as well segues to go from one controller to another, I also built my first really functional app, the To Do List, finally I looked at how to download information from the web and to manipulate data to display them to users. The next section seems also pretty heavy in content: I will discover how to work with different types of media such as images, maps and music. Exciting stuff, isn't it, but for now let me take a short break before plunging head first in new adventures with Swift 2!


Leave a comment

Login to comment a post