Swift language for IOS (1) and swift language for ios

Source: Internet
Author: User

Swift language for IOS (1) and swift language for ios

After continuous efforts, my brother also bought an Apple device and finally began my IOS journey. It was really distressing. in order to learn about IOS, I am a poor agricultural class, I spent a few months saving my pocket money and asking for help from my friends and family. I finally sold my MAC pro128G. I am not talking about the hard work. Let's talk about it next. The first time I used it, I did not talk about the work of IOS. The aluminum alloy body looks like it is a very clear identity, it is really smooth to use and comes with a lot of interesting applications. What surprised me most is that the video production software provides many templates, which makes it much easier for me to make small videos. The second is its multi-finger operation, cool, but it is really a headache when I get this book for the first time. I don't want to win it, as long as it can be clicked. In general, it is easy to use. I will not talk much about it. Next we will start to learn the Swift language.

We all know that Apple first used Object-C to develop Apple applications. Now Apple has improved Object-C and exited the Swift language, I believe that in the near future, Swfit will replace Object-C to become the mainstream language for IOS development. We will start our Swift journey.

First of all, let's explain the Xcode version I used. I use Xcode7.0. the syntax of this version is adjusted relative to 6.0.

1. Create a workspace

Open Xcode and click File> new> workspace in the upper left corner:

  

2. Create a Swift application

For convenience, we will create an OS etc project for getting started. When creating our application, the language must be Swift. 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 ")

4. Swift variables and constants

Var a = 1var B = 1var c = a + bprint ("c = \ (c )") let d = 2 // d = d + 1 // constant cannot change print ("d = \ (d )")

5. Swift data type

Var a = "Hello," // fully automatic type inference var B: String = "Hello," // print ("a = \ (a)") of the specified variable type )") print ("B = \ (B)") var c: Int = 100 print ("c = \ (c )")

6. Swift String concatenation

Var str = "Hello," str = str + "word! "Print (str) // str = str + 100 // str =" \ (str), word, \ (100) "print (str) var I: int = 100str = "\ (str)-\ (I)" print (str)

7. Swift Array

Var arr = ["Hello word! "," October 17, 2015 ", 100, 2.3] // defines the declared array and assigns a value to print (arr) var brr = [] // defines an empty array var crr = [String] () // define an empty array that can store String data. var drr = [Int] () // defines an empty array that can only store Int data.

8. Swift dictionary usage

/** Swfit dictionary usage is similar to the container object Map */var dect = ["china": "china", "year ": "75"] // define the dictionary and assign a value to dect ["sex"] = "Femail" // dynamically Add the dictionary print (dect) print (dect ["china"]) // obtain the Value saved in the dictionary based on the key

9. Swift for loop usage

Var arr = [String] () var brr = [String] () var crr = [String] () // closed range for I in 0... 100 {arr. append ("Item \ (I)")} // left closed right open for I in 0 .. <10 {brr. append ("brr \ (I)")} // left closed right open for var I = 0; I <10; I ++ {crr. append ("crr \ (I)")} print (arr) print (brr) print (crr) // traverses the array element for value in brr {print (value )} // while LOOP traversal array var I = 0; while I <crr. count {print (crr [I]) I ++} // for looping the element var dect = ["name": "china", "age ": "75"] for (key, value) in dect {print ("key = \ (key)") print ("value = \ (value )")}

10. Swift Process Control

For I in 0... 100 {if (I % 2 = 0) {print (I)} var a: String? = Nil // optional variable if var name = a // when the optional variable is null, {print ("name = \ (name)")} is not executed )")} a = "hpu" if var name = a {print ("name = \ (name )")}

11. Usage of functions in Swift

// By default, the function does not return the func sayHello (name: String) {print ("Hello \ (name)")} sayHello ("hpu") value ") var say = sayHello // declare the function say ("ios") // assign a value to call // return a value func getName () -> String {return "hpu"} var a = getName () print (a) // return multiple values func getName ()-> (String, Int) {return ("hpu", 106)} var (B, c) = getName () print ("B = \ (B), c = \ (c )") // pass the parameter with the returned value func setName (name: String)-> String {return name} var name = setName ("Hello HPU") print (name)

12. Object-oriented Design in Swift

Class SayHello {func hi (name: String) {print ("Hi \ (name)") }}var say = SayHello () say. hi ("hpu") // inherit class Hi: SayHello {// inherit the default Implementation of the parent class method} var hi = Hi () hi. hi ("ios") // inherit class Hello: SayHello {// override parent class method override func hi (name: String) {print ("Hello \ (name) ")} var hello = Hello () hello. hi ("ios") // initialize class A {func AA () {print ("aa")} class B: A {var name: String init (name: String) {self. name = name} override func AA () {print ("Hello \ (self. name) ") }}var B = B (name:" "") B. AA ()

Now the preliminary study on Swift is over. I believe you have some knowledge about the IOS Swfit language. Let's learn about IOS development together in the following days.

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.