Goal
- Playground Quick experience & learning resources sharing
- Project development quick Experience, understanding Swift BASIC Program Structure
Learning Resources
- Apple Official Blog https://developer.apple.com/swift/blog/
- Apple official Swift 2.0 ebook https://itunes.apple.com/us/book/id1002622538
- 2.0 Chinese Version http://wiki.jikexueyuan.com/project/swift/
- 100 Essential tips for swift, author Wang Wei, suggest buying an e-book http://onevcat.com
One, Playground
- Playground is a new feature introduced by XCDE 6
- Create a project to write and run programs that are designed to compile and publish programs
- The purpose of using Playground is to:
- Learning Code
- Experiment Code
- Test code
- and can visually see the results of the operation
- In addition, using Playground requires only one file, without the need to create a complex project
Quick Experience
let btn = UIButton(type: UIButtonType.ContactAdd)let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))view.backgroundColor = UIColor.lightGrayColor()btn.center = view.centerview.addSubview(btn)print(view)print(view.subviews)
Tips
- Some of the official learning resources are provided in the
playground
form of
- Create a file of your own
playgound
and be able to discover grammar changes the first time each version is upgraded
Second, the project development experience objectives
- Familiar with Swift's basic development environment
- Make a simple comparison with OC development
Code implementation
ClassViewcontroller:Uiviewcontroller {OverrideFuncViewdidload() {Super.viewdidload ()In Swift () instead of alloc/init in OCLet V =UIView (frame:CGRect (x:0, y: 20, Width: 100, Height: 100)) //[Uicolor redcolor]; v.backgroundcolor = uicolor.redcolor () //button let btn = Span class= "Hljs-type" >uibutton (type:. Contactadd) V.addsubview (BTN) //monitoring method Btn.addtarget (self, Action: "click:", forControlEvents:. Touchupinside) View.addsubview (v)} func click (Btn:uibutton) {print ( "point me up \ (BTN)")}}
Summary
- Not in Swift
main.m
, @UIApplicationMain
It's a program entry.
- Only files in Swift
.swift
, no .h/.m
distinction between files
- In Swift, a class is enclosed in a pair
{}
, with no @implementation
@end
There is no semicolon at the end of each statement, and in other languages, semicolons are used to differentiate between statements, in Swift, which is usually a line of code, so you do not use semicolons
A quick comparison with OC syntax
- corresponding in OC
alloc / init
()
- corresponding in OC
alloc / initWithXXX
(XXX: )
- class function calls in OC, in Swift, directly using the
.
- In Swift, the vast majority can be omitted
self.
, the recommendation is generally not written, can improve the understanding of the context (closures will be realized)
- Enumeration types in OC are used
UIButtonTypeContactAdd
, while Swift separates, Operation Hotkeys: 回车 -> 向右 -> .
in Swift, the prefixes of enum types can be omitted, such as: .ContactAdd
, but: many times no smart hints
- Listener method, directly using string to cause
- In Swift, use
print()
an alternative in OCNSLog
Swift Development Quick Experience