Swift basic grammar 01

Swift profile

  1. Swift2014 06 WWDC conference release
  2. Open source by the end of 2015
  3. IBM began to use Swift to do background development
  4. Swift cancels the precompiled instructions (including macros, such as Masonry)
  5. There is no parenthesis, which is closer to other languages
  6. 3 for the Foundation framework to make a major adjustment, remove the NS prefix
  7. Convert most of the Class to struct
  8. Access to the current object’s properties, you can not use self., it is recommended: generally do not use. Only when the closure is used
  9. Compiler directive cancelled
  10. Automatic derivation: the type of variables / constants will be based on the result of the execution of the right side of the symbol, the corresponding type
  11. Swift in the type of strict requirements, do not allow direct operation (will not do the default implicit conversion, all types are determined by the programmer)
Let x = 10 let y = 10.5 print (x + y) / direct error (Binary operator cannot be applied to operands of + type'Int'and'Double' print (x) + Int (y)) / / this is not an error, the output to -> `20` with OC type (int); y Swift Int (y) -> Swift constructor structure, basic data type does not exist, is the structure (safety and speed, considering the unity made grammar adjustment)

1 Button creation

BTN = UIButton let / / create button (type:.ContactAdd) view.addSubview (BTN) btn.center = view.center (btn.addTarget self, action: #selector (clickMe) for:,.TouchUpInside) / / button click event func clickMe (btn:UIButton) -> () {print (#function) print (BTN)}

2 View set the background color

Let v = UIView (frame: CGRect (x: 0, y: 20, width: 100, height: 100)) / / UIColor.red / / v.backgroundColor code to set the color = color picker directly set v.backgroundColor = #colorLiteral (red: 1, green: 1, blue: 0, alpha: 1) view.addSubview (V)
Swift basic grammar 01
2.1- set color.Png

3 ImageView set image

Let imageView = UIImageView (frame: CGRect (x: 0, y: 120, width: 100, height: 100) imageView.image) = UIImage (named: user) imageView.image = #imageLiteral (resourceName: user) view.addSubview (imageView)
Swift basic grammar 01
3.1- set ImageView picture.Png

4 Tips

/ / MARK: - XXXXXX
Swift basic grammar 01
4.1- marker (with horizontal line).Png
MARK: / / XXXXXX
Swift basic grammar 01
4.2- marker (without slash).Png
To modify the XXXXXX / / FIXME:
Swift basic grammar 01
06.png

5 options (Optional)

  • Optional is a major feature of Swift, Swift beginners are the most easily confused problems
  • When a variable is defined, if it is optional, the variable can have a value of a specified type, or nil
  • When a variable is defined, one is added after the type, indicating that the variable is optional
  • The default value for a variable option is the nil
    constant option with no default value, which is used to set the initial value to a constant in the constructor
  • Representatives from the optional – forcibly unpacking! Values to obtain the corresponding forced non null value, if it is nil, it will collapse! The programmer must be responsible for every one!
Let a: Optional = 10 let b: = 20 Int? Print (a) / Optional (10) print (b) / Optional (20) print (a + b) / of optional Value cannot be added up (type'Optional< Int> not unwrapped did you 'mean to use'; 'or!' '? (?) print a + B / 30!!)