Swift Language for iOS entry (i)

Source: Internet
Author: User

After continuous efforts, small brother also bought an Apple device, finally can start my iOS trip, it is really distressing, in order to learn iOS I this poor peasant class, save a few months of pocket money, plus to friends and family for help, eventually hurt the heart, sold a Mac pro128g version. Bitter force of things is not more said, the following chat, the first use of experience, the work of iOS really didn't see?, aluminum alloy body, looks very obvious identity, with a really smooth, and comes with a lot of interesting applications, of which I am surprised by the video production software, which provides a lot of templates, Let me make a little video convenient a lot. Next is its multi-finger operation, cool very, but the first time to get a laptop, it is really a headache, do not want to win as long as the point on the line, in general, with is cool. Gossip does not say much, below begins our Swfit language study.

As we all know, Apple was the first to use OBJECT-C to develop Apple applications, now Apple to improve Object-c, quit the Swfit language, I believe that in the near future Swfit will definitely replace the object-c become the mainstream language of iOS development, nonsense said, Let's start our Swfit tour.

First of all, I use the version of Xcode, I use the Xcode7.0 version, the relative 6.0 syntax should be a certain adjustment, you know.

1. Create a working space

Open our Xcode and then click on the top left corner of File->new->workspace:

  

2. Create Swfit Application

Here for convenience, we create an OS etc project to do introductory learning. When creating our application, the language must choose Swfit, the directory structure of the project:

  

3. Hello Word:

Import foundationprint ("Hello, world! " ) Print ("Hello, swift! " ) Print ("Hello, ios! " ) Print ("\ (2015) year \ (10) month \ (17) Day ")

4. Swfit Variables and constants

11= a + bprint ("c=\ (c)"2 // d = d + 1 // Constants Cannot change print ("d=\ (d)")

5. Swfit Data type

 var a =   " //  fully automatic type inference  var b:string =  " hello,  "  < span>  print ("  a=\ (a)   ) print (  " b=\ (b)  "  ) var c:int  = 100  print (   C=\ (c)   ") 

6, Swfit string concatenation

 var str =   " str  = str + "  word!   " print (str)  // str = str + +  //  str = "  \ (str), word,\ (+)   " print (str) var i:int  = 100  str  =  \ (str)-\ (i)   " print (str)  

7. Swfit Array

var arr = ["Hello word! ","October 17, 2015 , ",2.3]// define an array of declarations and assign values  = []// define an empty array var CRR = [String] ()// define an empty array that can hold data of type String var DRR = [int] ()// define an empty array that can hold data of type INT only

8. Swfit Dictionary Usage

/** Swfit dictionary usage is similar to container object map in Java*/var dect= [" China":"China"," Year":" the"]//define dictionaries and assign valuesdect["Sex"] ="FEMAIL"//Add a dictionary dynamicallyprint (DECT) print (dect[" China"])//the value stored in the dictionary according to key

9. Swfit for loop usage

var arr =[String] () var brr=[String] () var CRR=[String] ()//closed Interval forIinch 0... -{arr.append ("item\ (i)")}//left closed right open forIinch 0.. <Ten{brr.append ("brr\ (i)")}//left closed right open forvar i =0; I <Ten; i++{crr.append ("crr\ (i)")}print (arr) print (BRR) print (CRR)//iterating over array elements forValueinchbrr{Print (value)}//While looping through arraysvar i =0; whileI <crr.count{Print (crr[i]) I++}//for loop traversal of elements in a dictionaryvar dect = ["name":" China"," Age":" the"] for(Key,value)inchdect{Print ("key=\ (Key)") Print ("value=\ (value)")}

10. Swfit Process Control

 forIinch 0... -{    if(i%2==0) {print (i)}}var a:string? =nil//Optional Variablesifvar name=a//do not execute when an optional variable is empty{print ("name=\ (name)")}a="HPU"ifvar name=a{Print ("name=\ (name)")}

11. Usage of functions in Swfit

//function does not return a value by defaultfunc SayHello (name:string) {print ("Hello \ (name)")}sayhello ("HPU") Var say= SayHello//declaring functionsSay"iOS")//Assignment Invocation//returns a valueFunc GetName ()string{return "HPU"}var a=getName () print (a)//return multiple valuesFunc GetName ()(string,int) {return("HPU",106)}var (b,c)=getName () print ("b=\ (b), c=\ (c)")//The return value of the transmit parameter bandFunc SetName (name:string)string{returnName}var name= SetName ("Hello HPU") print (name)

12. Object-oriented design in Swfit

classsayhello{func Hi (name:string) {print ("Hi \ (name)")}}var say=SayHello () Say.hi ("HPU")//Inheritanceclasshi:sayhello{//inheriting the default implementation of the parent class method}var Hi=Hi () Hi.hi ("iOS")//Inheritanceclasshello:sayhello{//overriding the parent class method    Overridefunc Hi (name:string) {print ("Hello \ (name)")}}var Hello=Hello () Hello.hi ("iOS")//InitializeclassA {func AA () {print ("AA")    }}classb:a {var name:string init (name:string) {self.name=name}Overridefunc AA () {print ("Hello \ (self.name)")}}var b= B (Name:"Engineering Big") B.aa ()

Here about Swfit Preliminary study on the end, I believe you probably already have a certain understanding of iOS Swfit language, the next day Let us study iOS development together.

Swift Language for iOS entry (i)

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.