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 it is not the case, perhaps it is a small base, in short, we The general process of learning 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:

      The following interface appears after the first step, the arrow in the top left corner of the picture 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 step, in Click Next (Next), Then you can finish writing the Swift language happily.

        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 variables \ Constants (const  define)//1.1 define variables, use var to define var Xiang = "Xiang" println (Xiang)//1.2 define a constant using let to define let QM = 213let QQ = " Sadjflk "//1.3 variable constant naming specification: almost all Unicode encoded characters, except for some number symbols, arrows, dashes, and system keyword var?? = "haha" println (??) var var = "456"
2. Basic type//          type floating point type (double precision floating point single precision floating point type) Bohr string//c/c++ language: int float double bool Char[]//zai Swift language, the basic type has been almost rewritten, the first letter capitalized//SW Basic data types in IFT languages: Int Float Double Bool string//2.2var zs = 123let WHQ = "234"//[note] in the swift language, declaring a variable/constant without a type, but directly assigning a value, the compiler automatically infers Except for constants/variable types//2.3 named constants and variable variables  /constants: type name var lmt:string = "Liumingtao"//let lgz:int = "213"//2.4 Practice var zwd:int = 233/ /In Swift, integral type is int, system default 64 bit, if system is 32 bit, default is 32var wzb:int8 = -128//Int8 range: -128~127QQQ//[PRC:] Variable/constant var HxW = "Sjlfdasdjlk "var gz:string =" Guozhong "var zhengxing:int = 4564654let Wujie = 456let hah =" 45646 "var rkf = 3.5//[Note:] in swift, floating-point types The default is Doublevar rly:float = 2.5var cj:double = 2.6let 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 declares an array as a mutable array//let declares an array as an immutable group/ /create variable array var GST = ["Kechang", "Zhang Guodong", "Shaowen"]//the compiler will infer the type data, GST first is an array of arrays, and the data inside is string, so infer that the string array var ww = ["Wang Wei", 24] Add gst.append ("Bean Culture") Gst.insert ("Xu Junhui", atindex:1) gst//Delete gst.removelast () Gst.removeatindex (1) gst.removerange (Range ( Start:1, End:2)) gst//change gst[0] = Murrey gst//. Follow the table below to access//1. According to the following table range Access//... 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 element in the array and the location for (index, name) in enumerate (GST) {index NAME}//5. Dictionary//[Note:] Key-value pairs (key-value) are still retained in swift, but it is important to note that value must also be of the same type//declaration of a variable var sc = dictionary<string, int> ( //Add//swift, through the following table to access the elements in the dictionary, if there is no corresponding key in the dictionary, then is the addition of data sc["age"] = 12sc["name"] = 2sc//Delete Sc.removevalueforkey ("age")//Change// Use the following table to access the elements in the dictionary, if there is a corresponding key in the dictionary, then modify the value in the dictionary sc["name"] = 111sc//check//1.//2. Traverse for-infor (key, value) In SC {key Value}//bool type//bool type only true and Falsevar Ad:bool = Truevar Bs:bool = Falsevar B_zs:bool = true//s Tringvar b_hxw:string = "Huangxiangwang" b_hxw.uppercasestring//Capital b_hxw.lowercasestring//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 = 2switch B {case 0:b;case 1:b + 1;case 2:b + 2; Fallthrough//[Note:] In swift, by using Fallthrough you can force the execution of the next casecase 3:b + 3;case 4:b + 4;default: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 represented as a range, the range can be crossed, execution satisfies the first condition, and then jumps out of the loop var c = 25switch C {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")}//tuple can also be used as a switch condition var point = (1, 0) switch poin t {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 < 20; i++ {i}//2.for-infor J in 0...10 {j}//3.whilevar sum = 0//while conditions also require a condition with a return value of type bool, for example while 1 {} is error while S Um < 7 {sum + = 1}do {sum + = 1} while (sum < 10)//stored in arrayPut 10 tuple objects 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"] = ("WA Ng ", 15)//3. Use an array in the dictionary as Valuevar sk = dictionary<string, [string]> () sk[" Wujie "] = [" Wode "]var Yuanzu: (String, [Int] ) = ("Gaga", [55])

      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 ' hello ' +  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:string, Age:string)--(Stri Ng, String) {    return (name, age)}sayinfo ("Baby", "27"). 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!!!

More highlights: Shenzhen fan club Phoenix Entertainment Platform

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.