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