Some small Swift knowledge points and Swift knowledge points

Source: Internet
Author: User

Some small Swift knowledge points and Swift knowledge points

Some basic things of Swift were first understood and simply sorted out.

Import UIKitvar names = [] class ViewController: UIViewController, UIAlertViewDelegate {override func viewDidLoad () {super. viewDidLoad () var stringValue: String! // StringValue = "chw" // let str11 = stringValue. hashValue // var stringValue: Optional <String> if let str = stringValue {let hashValue = str. hashValue} let str1 = "swift" for k in str1 {println (k)} var dic = [0: 0, 1:0, 2: 0] var newDic = dic // Check dic and newDic [0] = 1 println (dic) // [0: 1, 1: 0, 2: 0] println (newDic) // [0: 0, 1: 0, 2: 0] var arr11: Array = [0, "lll", 0] var newArr = arr11 arr1 1 [0] = 1 // Check arr and newArr println (arr11) // [1, 0, 0] println (newArr) // [0, 0, 0] // String let heart = "inline" println (heart) // empty String let empty = "" let anotherEmpty = String () if empty. isEmpty {println ("empty")} else {println ("not empty")} // prefix suffix let str11 = "chengHong wei" if str11.hasPrefix ("ch ") {println ("prefix is ch")} if str11.hasSuffix ("wei") {println ("suffix is wei")} // case sensitive le T up = str11.uppercaseString println (up) // chenghong wei let low = str11.lowercaseString println (low) // chenghong wei // optional values explore var input: String? Var input1 = "18" var age = input1.toInt () if age! = Nil {println ("your age is" + String (age !))} Else {println ("error for input")} // traverse the array let arr = ["1", "2", "3"] //... it is equivalent to a range of [0, 2], which means 0 = <I & I <= 2 for I in 1... arr. count {println ("arr count is \ (arr. count) ") println (" arr object index \ (arr [I-1]) ")} // .. <indicates the semi-closed semi-open interval, which means [0, arr. count) (previously .., now I have changed) for I in 0 .. <arr. count {println (I)} for var I = 0; I <3; I ++ {println ("\ (arr )")} // array adds the element var arr1 = ["a"] arr1 + = ["B"] arr1 + = ["c", "d", "e"] Println (arr1) println ("arr1 is \ (arr1)") arr1 [0] = "f" println (arr1) var arr20 = [1, 2, 3, 4] arr20 [0] = 0 arr20 [1... 1] = [5, 8] // two elements 5 and 8 are inserted in array [1]. This function is only valid in the range State, if a [3] = [], the following error occurs: println (arr20) // [, 4] // how the generic type is used to define the storage of the container. There is also an abbreviation of an array, which is more readable, but essentially the same. Now you cannot add non-Int elements to the array. This sounds terrible, but it is very useful. You no longer need to use APIs to record which elements are returned from a method or stored as attributes in the array. You can tell the compiler that the compiler is more intelligent in error checking and can optimize it early. Let arr2: [Int] = [1, 2, 3, 4] // The array can only contain the int type let arr3: [Float] = [1, 2, 2.3] println ("arr3 is \ (arr3)") let arr4: [String] = ["1", "chw ", "love"] println ("arr4 is \ (arr4)") // you can add elements to a variable var array. let declares that elements cannot be added, in Objective-C and Cocoa, you can select two different classes (NSString and NSMutableString) to specify whether the string can be modified, whether the strings in Swift can be modified is determined only by defining the variables or constants, and the unification of various types of variability operations is achieved. Var arr5: Array = [1, 2, 3, "cheng"] arr5.append ("chw") println ("arr5 is \ (arr5 )") // dictionary let dic11 = ["name": "chw", "sex": 1, "height": 175.0] println ("This is a dictionary \ (dic11 )") // Swift = is var a1 = 5 var a2 = 5 if a1 = a2 {println ("=")} else {// println ("! = ")} // String concatenation let cheng = "c" let hong = "h" let wei = "w" let chenghongwei = cheng + hong + wei println ("my name is \ (chenghongwei) ") // obtain the remainder println (8% 3) // = 2 println (8% 2.5) // 8% 2.5 = 8-2.5*3 = 0.5 decimal places can also be obtained. // swith statement var I = 14 switch I {case 0... 10: println ("range is 0-10") case 11... 20: println ("range is 11-20") case 21 .. <30: println ("range is [21-30)") default: break} var name = "chw "Switch name {case" cheng ": println (" name is cheng ") case" hong ": println (" name is hong ") case" wei ": println ("name is wei") default: println ("someone else") break} // A Preliminary Exploration of the closure names = ["Chris", "Alex", "Ewa ", "Barry", "Daniella"] func backwards (s1: String, s2: String)-> Bool {return s1> s2} // UI control let lab = UILabel (frame: CGRectMake (100,100,200,100) lab. text = "Hello Chw" lab. backgroundColor = UIC Olor. greenColor () lab. textAlignment = NSTextAlignment. center self. view. addSubview (lab) let btn = UIButton (frame: CGRect (origin: CGPointMake (100,250), size: CGSizeMake () btn. backgroundColor = UIColor. grayColor () btn. setTitle ("Chw btn", forState: UIControlState. normal) btn. addTarget (self, action: "btnClick:", forControlEvents: UIControlEvents. touchUpInside) self. view. addSubview (btn)} // TODO: --- Func btnClick (sender: UIButton !) {Println ("chw 111") var alert = UIAlertView () alert. title = "CHW" alert. delegate = self alert. message = "This is a alert! "Alert. addButtonWithTitle ("Cancle") alert. addButtonWithTitle ("OK") alert. show ()} // MARK:-// MARK: --- AlertViewdelegate func alertView (alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {println ("buttonIndex: \ (buttonIndex )") if buttonIndex = 0 {println ("Cancle Btn! ")} Else if buttonIndex = 1 {println (" Sure Btn ")} // MARK:-override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}

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.