Swift starts learning _01 from scratch

Source: Internet
Author: User

I. INTRODUCTION

I'm talking about the 0 basis of the Swift Language 0 Foundation, the previous development has been used OC, did not contact Swift, recently saw everyone learning some swift, just a little time recently decided to smoke an hour or so a day to learn a little bit of swift. Prepare to write a little essay every day, Record their own swift learning process, not to teach you swift, but simply want to urge their own learning, most of which belongs to the individual inference or understanding, but also hope to understand you more advice.

Two. Swift First day

①. String

var name = "Chris Paul"//var creates the variable, automatically infers the type, the string in swift, does not require the @ symbol, and the end does not require a semicolon

var name1:string = "Chris Paul"//after the variable name to: separate, you can specify the variable data type, temporarily did not find out what difference nsstring and String, anyway did not error

Print (name)//output function, (want to wrap with the println function), like in OC NSLog, where the parentheses are written directly to the parameters to be output, you can also use "", in the middle to add the content to output

Let name2 = name + "" + name1//You can use the plus sign to connect characters, using the same method as in JS, the constant defined

Let number = 30

Let string = Name2 + "" + string (number)//The last plus sign is the strong-turn type in swift, which means to convert number to string

Let name = "Chris Paul"

Let teststring = "name"//with \ () refers to the output as: name is Chris Paul

② Array

var array:[string] = ["Dhggmng", "poimng", "Cxnmznb", "dqonmdw"]//swift create an array, which is said to have only one data type in an array in swift, but I do not specify a type, the array of various Data mixed put, also no error! So this place needs further research.

Print (array[1])//Fetch array object can be obtained by subscript

var array1 = []//Create an empty array

if (arrar1.isempty) {

Print ("Array is empty")//array has the IsEmpty property to determine if the array is empty, the same as the previous Array1.count = = 0, the return value is bool

}

Array1.append ("DOOMGNN")//Use the Append method, you can add an object to the array, the object will be placed at the end of the array, you can also use + =, the method is as follows

Array1 + = "DOMNGNG"//Feel this method good abuse ah, and + = can also directly put two arrays together, this is brutal AH

Array1 + = ["Mnlaogn", "Zbbnnaq", "UQOMNGN"]

ARRAY1[1...3] = ["CCCC", "MMMM"]//here ... The meaning of personal understanding is 1 to 3 meaning, this operation means that the subscript from 1 to 3 have to replace the tune, the following two items are written, so the new array is one less.

Array1.insert ("Hello", atindex:1)//insert "Hello" in the position of subscript 1, feel swift this var (variable) Good intelligent Ah, all become variable.

var array2 = array1.removeatindex (0)//Remove the element labeled 0, it is important to note that the print array2, the result of printing is the removed object, not the removed array, but at this time the array1 has changed, Actually want to an object, you can directly go to invoke the method, no need to receive.

For STR in array2{

Print (str)//The use of for...in here, after the test, for the back can be arbitrarily assigned to a thing, can be indexed to, do not specify the type, it is very strange ah

}

For (index, value) in enumerate (array1) {

println ("Item \ (index + 1): \ (value)")//The result returned in this way will be returned in the form of a key-value pair, which will look more comfortable.

}

Someints.append (3)//someints currently contains an int value

Someints = []//Now someints space-time array, but he will become int[], I feel this rule is very strange

var array2 = Array (count:3, Repeatedvalue: "What")//Create an array with three "what" strings

var array2 = [String] (Count:3, Repeatedvalue: "What")//This allows you to specify the type of data contained in the array, creating the same effect as above.

Conclusion: With a little more than one hours of time to simply learn the array and string, the difference between the feeling and OC is really big, accept a bit difficult, slowly. What is wrong with the place, but also hope to understand the proposed, I am good to correct, thank you! -CP

Swift starts learning _01 from scratch

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.