Swift Learning II

Source: Internet
Author: User

Reference Blog: Http://blog.csdn.net/jaywon

Swift does not automatically assign an initial value to a variable, you must manually add the initial value

Arrays Array
Let Array:array = []; Declares an empty array
var array1 = string[] () declares an array of strings
Add a new element to the end of the array
Array.append ("new element")
Array + = "new Element"
Array + = ["new element 1", "new element 2",...]
Adds a new element to the specified position in the array
Array.insert ("new element, Atindex:index)

ARRAY[4...7] = ["new element 1", "new element 2"]//Replace element, can be less than the original
, which will result in a reduction in the number of elements in the array
Array Delete last element
Array.removelast ()
To delete an element at the specified location
Array.removeatindex (Index)
Delete all the elements
Array.removeall ()
Iterating through an array
For item in array {
println (item)
}
for (Index,value) in enumerate (array) {
println ("item\ (index+1), value\ (Value)")
}

Dictionary Dictionary
var dictionary = dictionary<string:float> () creates a dictionary, key is
String type, value is a floating-point type
adding/Modifying a dictionary
Dictionary[key] = value Traversal dictionary, if the corresponding key is found to be modified,
Not found even if added
Dictionary Delete key value pairs
Dictionary[key] = Nil
Dictionary.removevalueforkey (Key)
The traversal of a dictionary
for (Key,value) in dictionary{
println ("\ (key): \ (value)")
}
Traverse the dictionary for all keys
For key in dictionary.keys{
println (Key)
}
Traverse the dictionary for all the value
For value in dictionary.values{
println (value)
}

Optional type Optional This value is either present and equal to X, or does not exist at all
Let Numberstr = "123"
Let Convertnumber:int? = Numberstr.toint () present is turned into 123, does not exist
Then turn to nil.
Nil in Swift is not a pointer, it represents a value of a particular type does not exist, optional type of any type
can be assigned to nil, not limited to object type, and more secure
Rules:
1. Nil in Swift cannot be used for non-selectable types Eg:var str = nil is wrong
2. If a constant or variable in your code needs to be adapted to a situation that does not exist, be sure to declare it
When the optional type
3. If an object of the defined optional type does not provide a default value, the object is automatically set to nil
Eg:var str:string? Without assignment, the default value is nil.
var str:string?
Let HashValue = str?. HashValue//must add a question mark indicating whether the optional type is
The method or property that should be followed

Unpack (! If you determine that the value of an object of an optional type must exist, then use!
To fetch its value, or optional the Binding
Use "! ”
Let str:string? = "12345"
println (str)
Let Descstr = str!. HashValue
Use Optional Binding to determine if a constant exists
If let svalue = str {
Let Descstr = Svalue.hashvalue
}

Implicit unpacking
After the optional type name, at the time of definition? Change it!
Let str:string! = "123456"
println (str)
Let Descstr = Str.hashvalue

Strings string
String equality can be used = =
String prefix String.hasprefix
String suffix String.hassuffix
The number of characters in the string countelements (String)

Swift Learning II

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.