Learning the Swift language of iOS

Source: Internet
Author: User
Tags float double switch case

         A long time have not come to this familiar and unfamiliar place, think about already two or three months, but I believe that will often come, because the busy study has passed, the rest is to good review of the good, well familiar with the knowledge we learned before, will they have organized in series together.

In the past nearly three months, whether it is busy or what, perhaps in some people's view this is a non-human life, but the individual have their own law bar, in the face of their never contact with the code and grammar, at first really is a little bit of fear, but slowly there is no such feeling, Gradually also found that the code is not so terrible, it is not so difficult. After three months and code in the run-in I found that I no longer hate, no longer resist him, can also be said to be interested in him, I found myself deep inside began a strong desire, there is a constant call voice that is to supervise me, and always reminds me that I must learn the iOS, Do not let those who care about me care about my disappointment and sadness. Here is the knowledge we have learned to do a small chain of knowledge!

In the beginning of the class time, I thought that only I am not the professional, but gradually found that, in fact, is not so, there are many are not the professional, there are many and I am the same 0 basis, so I began to have confidence, but fortunately, at the beginning we have the most basic C language step into the programmer stage, And then with nearly one months of C language learning, will usher in OC to learn, just start time, look at the blackboard above the English letter, it is really confused, slowly found that the unfamiliar English letters, grammar, and so looked like there is a feeling, and also to their interest, began to want a good study, Next is the most important UI stage, it can be said that the UI allows us to learn the longest period of time, all kinds of problems, but the problem is not terrible, there is a terrible problem not to ask, but this is not to say I, because I know there is a problem not to ask, it is irresponsible for themselves, Although there is a time to ask the question is as simple as adding one, but then what, my purpose is, whether simple or complex, I would like to give him to understand that in the end I basically completed my goal, I know today I made my own first app (of course, is currently under review stage, is expected to appear in the Apple Store this week), until today, we touch the new language, that is the swift language, look at the language of last June, there is a curious feeling, it would be very difficult, but the fact is not so, perhaps it is a small base, in short, The general process of our study is the C language--oc--ui stage--swift language These chunks, then step into today's theme it:

        Today is the first day of contact with Swift language, feeling full and happy, below I will introduce you to the swift language of several grammar and considerations.

        First, the creation of Swift language engineering

      It is mentioned here to emphasize that Swift language and OC are different from the creation of UI, as follows:

      

      The direction the arrow refers to is where the SWIFT project is created, next to it is the UI and the OC Project.

        Second, Swift language project name

The name and UI of the Swift language project and the OC are almost exactly as follows:

      after the first step, the following interface appears, the arrow in the top left corner of the image is the project name, the upper right Arrow is the platform, of course, if you want to return then just click on the lower left ARROW to the direction of the Cancel button, as for the lower right corner of the arrow is the next operation, After clicking Next, you can happily complete the writing of the Swift language .

        III. grammatical structure of swift language

      The first thing to note is that the swift language and the C language, and the OC and UI syntax structure are different, as follows:

1. Methods for declaring/defining variables and constants:      

1. Define variable \ constant (const define)//1.1 define variable, use var to define var Xiang = "Xiang"println (Xiang)//1.2 defines a constant using let to define let QM = 213 Let QQ = "sadjflk"//1.3 variable constant naming specification: almost all Unicode encoded characters are supported, except for some number symbols, arrows, dashes, and system keyword var?? = "Haha"  println (??) var var = "456"//2. Base type//type float (double precision floating point single precision floating point type) Bohr string//c/c++ language: int float double bool Char[]//zai Swift language, base This type has been almost rewritten, capitalized//swift the basic data type in the language: Int Float Double Bool string//2.2var ZS = 123  Let WHQ = "234"//[note] in the swift language, declare the variable /constant If there is no type, but a direct assignment, the compiler will automatically infer the type of the constant/variable//2.3 named constant and variable variable/constant: type name var lmt:string = "Liumingtao"//let lgz:int = "213"//2 .4 practice var zwd:int = 233//in Swift, the integer is Int, the system defaults to 64 bits, and if the system is 32 bits, the default is 32var wzb:int8 = -128//Int8 range: -128~127QQQ//[PRC:] Variable /constant var HxW = "Sjlfdasdjlk" var gz:string = "Guozhong" var zhengxing:int = 4564654  Let Wujie = 456  Let hah = "4 5646 "var rkf = 3.5//[Note:] In swift, the float type defaults to Doublevar rly:float = 2.5var cj:double = 2.6  Let DFR = rly +  float ( CJ)//[Note:] Implicit conversions are not supported in the swift language and can be displayed in the form of conversions: type (variable/constant)       

      2. Declaration and use of arrays and dictionaries

4. Array array//[Note:] in OC, arrays hold elements that are based on NSObject type objects and object types can be different. In swift, the array must hold an object of the same type//define an array var Lc:array =[String] () Lclet Sxr:array =[String] ()//var declaration array for variable array//let declaration array for immutable array//create variable arrays var = ["Kechang", "Zhang Guodong", "Shaowen"The]//compiler infers the type data, and GST is first an array of arrays, followed by a string of data, so infer that the string array var ww = ["Wang Wei", 24]//Increase gst.append ("bean Culture")) Gst.insert ("Xu Junhui", atindex:1) gst//DeleteGst.removelast () Gst.removeatindex (1) Gst.removerange (Range (start:1, End:2) gst//gst[0] = "Murrey"  gst//check//1. Access gst[1 ]//2 according to the following table. Access according to the following table//... Represents a closed interval Gst[range (start:0, end:1 )]gst[0...1 ]//. < represents an open interval gst[0..<1 ]//3. Traverse for name in  GST {name}//4. Gets the elements in the array as well as the location for (index, name) in  enumerate ( GST) {index NAME}//5. Dictionary//[Note:] The key-value pair (Key-value) is still retained in swift, but it is important to note that value must also be the same type//declare a variable var sc = dictionary< String, Int>  ()//Add//swift, the following table accesses the elements in the dictionary, if there is no corresponding key in the dictionary, then it is added data sc["age"] =  sc["name"] = 2  sc// Delete Sc.removevalueforkey ("Age" )//Modify the elements in the dictionary through the following table, and if there is a corresponding key in the dictionary, change the value in the dictionary sc["name"] = 111  sc//Check//1. Subscript Method// 2. Traverse for-infor (key, value) in  sc {key Value}//bool type//bool type only true and Falsevar Ad:bool = Truevar Bs:bool = f Alsevar B_zs:bool = True//stringvar b_hxw:string = "Huangxiangwang"  b_hxw.uppercasestring//uppercase B_HXW.LOWERCASESTR ing//lowercase               

      The above code is the definition of swift language arrays and dictionaries, it is obvious from these can be seen, it and the C language and other language definition methods are different, so to be familiar with these need more effort, no matter what we all need to write hard, for this we also do a lot of practice, specifically as follows:

Branching structure//1.if-else//if Condition {//statement block//}var a = 5if (A = = 5{a}//[Note:] The condition must be of type bool//2. Switch-casevar B = 2switchb {case 0: B;case 1: B + 1; Case 2: B + 2; Fallthrough//[Note:] In swift, you can force the execution of the next casecase by using Fallthrough 3: B + 3; Case 4: B + 4;d Efault: B + 5;} [Note:] Write switch must add default//[Note:] in swift, switch case strengthened, do not need to add break//[practice after each case) to a date, xx years xx month xx day, calculate this day is this year of the first day?// The case in switch can be expressed as a range, the range can be crossed, execution satisfies the first condition, and then jumps out of the loop var c = 25switchc {case 0...25 where c! = 29://uses where to double-judge println ("between 0~30") Case 20...80: println ("Between 20~80") Default: println ("No This number"}//tuples can also be used as a switch condition var point = (1, 0) Switch  point {case (0, 0 ): println ("origin" ) case (_, 0 ): println ("on X axis" ) case (0 , _): println ("On Y axis" ) default : println ("other points" )}//3. Loop structure//1. forfor var i = 0; I < i++  {I}//2 . for-infor J in 0...10  {j}//3.whilevar sum = 0//While condition also requires a condition that returns a type bool, for example while 1 {} is error while sum < 7 {sum + = 1 }do  {sum + = 1 } while (Sum < )//store 10 tuple objects in the array var shuzu:array =  [String , Int] () for var i = 0; I < 10; i++  {shuzu.append ("hah")}shuzu//uses tuples in the dictionary as Valuevar sd = dictionary<string, (String, Int);  () sd["name"] = ("Wang", "")//3. Use an array in the dictionary as Valuevar SK = dictionary<string, [String]>  () sk["Wujie"] = [" Wode "]var Yuanzu: (String, [Int]) = (" Gaga ", [+])         

      and at the end of the class, we further learned that the function statements in the SWIFT language include multiple arguments and single parameters, as shown in the following code :

function//function definition/*func function name (parameter list)--(return value type) {    code block}*/func SayHello (name:string) --(String) {    return ' he Llo "+ Name}sayhello (" Qiaodaye ")//Multiple parameter parameters separated by a comma func Saybyebye (name:string, name1:string) --(String) { return    name + "Byebye" + Name1}saybyebye ("Qiaodaye", "Guozhong")//return value for multiple parameters return value for tuple type func sayinfo (name:str ING, age:string) (String, String) {return (name, age)}sayinfo ("Baby", "0").
           

        These are what we learn today, although not many, but after all, is the first contact it, or need a long time to practice and summarize, each contact a new knowledge point, although the classroom hearings, but more need a lot of practice and summary, so look at the code of the small partners to work together, Build belongs to our tomorrow, good today wrote here, tomorrow I will continue to work hard, refueling, refueling!!!

Learning the Swift language of iOS

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.