"We all love Paul Hegarty." Stanford IOS8 public class personal note 8 String, Type Conversion, assertions

Source: Internet
Author: User

The difficulty in handling strings is the indexing of strings and other issues such as creating strings, and how to create substrings of a string.


Strings are made up of Unicode, you don't think of substrings of Unicode strings as Unicode characters, they break them into Unicode, so we can't index them like arrays, because we're going to see that some of the characters are made up of more than one character. This will change the original meaning by splitting a full semantic character into two characters. So the string is indexed by a different type, which is string.index. Note Advance this is a function, not a method, it skips the entire character instead of one. As shown in the example, index outputs "L".

In addition, the splice in the string and the splice in the array are similar, merging one string into another string. The value of s in the example now becomes Heabcllo.

Finally, we use range to get the substring of s, which cannot be used directly with int when intercepting the starting stop position, still must adopt the method of advance, as the last truncated substring in the example is EABCL.


Remember the Rangeofstring method used in the Calculator project, which returns a range of type optional. We can take the entire method to get substrings in a string, such as the first example gets the decimal part of a floating-point number. or replace or delete this part, but only if you get the range you need.


String has some other methods, all based on the index of the string, with the description, join method we have seen before. It is worth noting that the string operation is only ToInt and not todouble, because the structure of double is more complex, you need to specify some details such as how many digits after the decimal point and so on, need additional parameters. But int does not exist, of course toint return value is selectable, if you let "hello" call ToInt, then obviously will return a nil.


There are also methods for implementing type conversions through initialization. It is interesting to note that you can use the initialization method to implement the interaction of the array and the string, but be aware that the elements of the converted array are Unicode characters in the string.

We can also convert floating-point numbers into strings by means of the \ () Call method.

Finally, we will talk about a debug-related method. Assertions


Assert this method takes a closure as its first argument, the second argument is a string, and the first closure returns a false, then the string is printed to the central console.

As in the example we want a function to be empty, if it crashes the program, use Assert, and when the function is empty, it will print the following string to the console so you know where the problem is.


"We all love Paul Hegarty." Stanford IOS8 public class personal note 8 String, Type Conversion, assertions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.