Strings String
1. Disclaimer
An empty string
let emptystring = "
anatherstring = string ()
if emptystring.isempty {
print (" Empty, I Rub ")
}
2. Escape character
escape character, \ \ (backslash), \ t (horizontal tab), \ n (line break), \ r (carriage return), \ "(double quotation mark), \ ' (single quote).
///Unicode scalar, written as \u{n} (U is lowercase), where n is any one to eight hexadecimal digit.
let Windoe = "\" I want to be the man of the king of Thieves \ "-Luffy" to
dorllar = "\u{24}" "$" make
Blackheart = "\u{2665}" "♥" Make
sparkl Ingheart = "\u{1f496}" "Red Love"
3. String concatenation
Variable and immutable string
var varstring = "MKJ"
varstring + = "CJJ"
//let letstring = "MKJ111"
//letstring + = "CJJ"
//in Objective-c and Cocoa, you specify whether the string can be modified by selecting two different classes (NSString and nsmutablestring), whether the string in Swift can be modified only by defining a variable or constant, It realizes the unification of various types of variability operations. The
String type//Swift represents a collection of character (character) type values for a particular sequence. Each character value represents a Unicode character. You can use the for-in loop to iterate through each character in the string here and before the different needs call characters to traverse for the
charater in "he's a". Characters {
print (charater)
}
Calculate the number of strings let
countingstring = "NDSD dsfdgg kjjkfdfdlf;"
Print ("contingstring is number of \ (CountingString.characters.count) charaters")
//string concatenation let
string1 = "111" Let
string2 = "222"
var string3 = string1 + string2 let
charater1:character = "?"
String3.append (Charater1)
//String formatting \ () let
num1 = 3
print ("\ (NUM1) * 2.5 Yes \ (Double (NUM1) * 2.5)") c22/>/*
h
e
i
s
a
contingstring
is number of charaters 3 * 2.5 Yes 7.5
* *
4. String comparison and capitalization
String comparison equality directly with = = let
Quato = "let Quate =" the same as "
if quate = = Quato {
print (" Same ")
}
//And O C, judging the prefix with Hasprefix and hassuffix
//String case let
normal = "Could help Me" let
upstring = normal.uppercased () let
downstring = normal.lowercased ()
Arrays Array
1. Disclaimer
1. The construct
//shoppinglist variable is declared as "an array of string value types", which is recorded as [string]. Because this array is specified to have only a data structure of string, only the string type can be accessed
var shoppinglist:[string] = ["Milk", "apple"]
// Swift can automatically infer the type so it can be used without the array declaration
2. Get the quantity and determine if it is empty
2. Get the quantity/determine if the blank
print ("Shopping list has \ (shoppinglist.count) items")
if Shoppinglist.isempty {
Print ("Array is empty")
}
3. Array access, adding delete
3. Adding data to delete and access
shoppinglist.append ("reveal")
shoppinglist + = ["Photo shop", "Xmind"]
var item = SHOPPINGLIST[0]
//Can be directly interspersed with expansion
shoppinglist[2...3] = ["QQ", "Wechat", "MTJF"]
print (shoppinglist)
Shoppinglist.removelast ()
shoppinglist.removefirst ()
print (shoppinglist)
4. Traversal and enumeration traversal
4. Traverse
for item in Shoppinglist {
print (item)
}
//Need all information/
*
1th is Apple
(offset:0, element: "Apple"
2nd is QQ
(offset:1, element: "QQ")
3rd is WeChat
(offset:2, element: "WeChat")
the 4th one is MTJF
. (Offset:3, element: "MTJF")
*/For
value in shoppinglist.enumerated () {
print ("s \ (Value.offset + 1) is \ (value.element)")
print ( Value)
}
5.repeat Creating an array
5. Build Data
var intarr = [Int] ()
print ("Array of intarr has elements \ (intarr.count) items")
//Repeat Create
VA R Repeatarray = [Double] (repeatelement (3.0, count:5))
print (Repeatarray)
Set set
1. Unordered arrangement
Set set He is unordered
var setmusics:set<string> = ["JJ", "KK", "LL", "OO"] for
item in Setmusics {
print (item) c5/>}
print ("\ n"
)
/* OO
JJ
KK
LL
*
///Ordered output for
item in setmusics.sorted ( ) {
print (item)
}/
*
JJ
KK
LL
OO
*/
2. Basic operation
Let Odd:set = [1,3,5,7,9] let
even:set = [0,2,4,6,8] let
deltad:set = [1,2,7,8]
//Union two set all elements create a new set Fit
Print (odd.union (even). Sorted ())
//Intersect intersection
print (odd.intersection (Deltad). Sorted ())
/ /Exclusiveor anti-intersection
print (odd.symmetricdifference (Deltad). Sorted ())
//Subtract call sequence and another sequence to remove intersecting parts create a new collection
var aaa:set = [1,2,3,4,5] let
bbb:set = [2,3,4]
print (aaa.subtracting (BBB). Sorted ())/
*
[0 , 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 7]
[2, 3, 5, 8, 9]
[1, 5]
*/
Dictionary Dictionary
1. Basically the same as OC's statement is as follows
Dictionary, this is basically the same as OC
//Create
var namedict = dictionary<int,string> ()
2. Basic Operations Update and create
To update the dictionary or create a value has the following two kinds, a direct access to key there is a updatevalue, can not find the time there will be feedback, into the next wave operation, but actually there is update, no add
namedict[1] = "MKJ"
namedict[2] = "CJJ"
print (namedict)
//The following method returns the original value before the update
if let value = Namedict.updatevalue ("HEHE", Forkey:2) {
print (namedict[2]!)
Print (value)
}
//If present, return the original value as above, if judgment is established, update old value, but no case return is nil, cannot copy to let, so if fail, also add new key value, But if go fails the logical
if let value = Namedict.updatevalue ("Meile", forkey:0) {
print (namedict[0]!)
}
else
{
print ("does not Exist")
}
print (namedict)
//Removevalueforkey same/
*
[2: "Cjj", 1: " Mkj "]
HEHE
cjj
does not exist
[2:" HEHE ", 0:" Meile ", 1:" MKJ "]
*/