Swift arrays are used with dictionary type operations

Source: Internet
Author: User

var studentlist: [String] = [" Zhang San "," John Doe ", " Harry "];


// array insertion

studentlist. Insert (" Liu Bei ", Atindex: 0)


// array append, only one element can be appended per call

Studentlist. Append (" Zhao Yun ");


// array append multi-element

Studentlist + = [" Guan Yu "," Zhang Fei "];


// array traversal

for student in studentlist {

Print (student);

}


Printing results:

Liu bei

Tom

John doe

Harry

Zhao Yun

Guan yu

Zhang Fei


// dictionary type if you take a type example from Java , similar to Map

var studentdictionary: dictionary <, string > = [102 : " Zhang San ", 105 : " John Doe , 109 : " Harry ]

Print (" dictionary traversal Keys");

for StudentID inch studentdictionary. Keys {

Print (" student ID: \(studentid)");

}

Print (" dictionary traversal Values");

for Studentname inch studentdictionary. Values {

Print (" student name :\(studentname)")

}


Print (" dictionary traversal key-value pair form ");

for (StudentID, Studentname) inch studentdictionary {

Print (" student ID:\(studentid) student name :\(studentname )")

}


The traversal of String type is not supported in this way after the current xcode7, if using xcode6.2 can be compiled by

var str: String = "10.9989";

for number in str {


}


This article is from the "10271496" blog, please be sure to keep this source http://10281496.blog.51cto.com/10271496/1750322

Swift arrays are used with dictionary type operations

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.