Swift Chapter I: Simple data structure

Source: Internet
Author: User


First we can use playground to encode, and will display the corresponding encoding information in real-time, so that we do not have to run the program every time to display the output of the things Oh, but also convenient for some of the statements of the verification, this is a better


var and let

The former is a variable modifier and the latter is an immutable

Literally, we can make a good distinction.

Let name = "Kutian" var anothername = "Xiaohua" anothername = "Kutianxiaohua"


Commonly used types, and other languages are basically the same, there are several main:

1.int type 2.float,double type 3.String type 4.Boolean type

When we declare a variable, we can choose to initialize its type.

But here, our bool type is only true and False oh , as for 0 and 1, we still have to distinguish between the OH

var maxValue = Uint8.maxvar MinValue = Uint8.minvar Doublevalue = 3.14var Anothervalue = doublevalue + Double (MinValue) var stringvalue:string = "SwH" var isdouble:bool = Trueif isdouble {println ("true")}


The next step is to briefly introduce some syntax for the string.

The interesting thing here is that the string can be added to another string to get a new string.

That is, with "+" to splice strings, so "+ =" is also useful

And then there are some of its properties.

For example: to determine whether it is empty, to traverse characters in a string, to count the number of characters in a string, to determine whether a string is equal, to determine whether to use * * as the prefix, etc.

 var name =  "Sun wanhua" var hello =  "hello " var greet =  hello + nameprintln ("hello \ (name)  !") When we output, only the variable words are written directly, if the "" inside will add \ (variable name) var string =  "my name is " string +=  nameprintln (String) var emptystring: string =  ""//method of invoking the string directly, using "." Syntax to call IF EMPTYSTRING.ISEMPTY {    PRINTLN (true)} else {     println (False)}for char in  "Sunwanhua"  {    println (char)} var stringcount =  "Kutian"//With count (variable name) to get the length of the current variable var strcount =  "\ (COUNT ( Stringcount)) "println (strcount) var str1 = " Sun wanhua "var str2 = " Sun  Wanhua "//Here we do not need to use Isequalto: such as grammar, oh, direct" = = "If str1 == str2 {    println (" Equal string ")} else {    println (" unequal string ")}var prefixstring =  "Lo" var productstring =  "Long long" var nextstring =  "Lang  lang "If productstring.hasprefix (prefixstring)  {    println (True)}  Else {    println (False)}if nextstring.hassuffix ("ng")  {     println (True)} else {    println (FALSE)}


The next is the array, oh, it's pretty simple.

 var stonehero:[string] = ["Blood mage",  "Shaman",  "Warrior",  "Druid",  "Paladin",  "Priest",   "Warlock",  "Rogue",  "Hunter"]//directly modifies the value of elements within the array stonehero[0] =  "Mage" for hero in  Stonehero {    println (Hero)}//here involves a new data structure: Ganso (index , value), let's Imagine (404  ,  "error")//If you use the meta-ancestor traversal, you need to do a good job, then the array will need to resemble the form of the progenitor enumerate () for  (index , value)  in  enumerate (Stonehero)  {    println ("\ (value) is located at: \ (index + 1)")} println (Stonehero.count) if stonehero.isempty {    println (True)} else  {    println (false)}stonehero.append ("Hotel boss") stonehero += ["Hotel hostess"]//[4...6] Should be the meaning of the closed interval, is 4,5,6//and there is a kind of writing is half open interval [4..<6], is 4,5stonehero[4...6] = ["Why",  "do" Stonehero.insert ("You",  atindex: 6) Stonehero.removeatindex (4) Stonehero.removelast ()//The following means to create an array, and several elements of the same type and value var threedoubles = [Double] (count: 3, repeatedvalue: 1.0) Var anotherthreedoubles = array (count:  3, repeatedvalue: 2.5)//the same type of array can also be added to each other Oh var sixdoubles = threedoubles +  anotherthreedoublesfor value in sixdoubles {    println (Value)}


And then, it's a dictionary, come and take a look.

 var country = ["cn":  "China",  "Us":  "USA",  "JP":  "Japan"]var  Anothercountry: dictionary <string, string> = ["cn":  "China",  "US":   "American",  "JP":  "Japan"]var emptydictionary = [:]//so, after the Yuan Zu, it is very good to control the dictionary, haha, Looks like the Yuan Zu and the dictionary is the matching play for  (shortname, longname)  in country {    var  result = shortname +  ":"  + longname}for shorname in country.keys  {    var result = shorName}for longName in  Country.values {    var result = longname}var shortnamearray  = array (Country.keys) Var longnamearray = array (country.values) country["cn"]country[" JP "]country.countcountry.updatevalue" ("Porcelain", forkey:  "cn") Country["cn"] =  "Xiaowang" Country.removevalueforkey ("cn") country["JP"]&NBSP;=&NBSp;nil 


In fact, the data type is OK, with o-c inside basically much the same, may be more simplified writing, oh, should also not consider the use of memory, it also let more people start the Ah, it seems quite good


Well, first write so much, back to study when the time to write OH



This article is from the "Neusoft iOS Alumni Group Technology Blog" blog, please be sure to keep this source http://neusoftios.blog.51cto.com/9977509/1669466

Swift Chapter I: Simple data structure

Related Article

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.