Swifit Sixth Day
Today I am ready to take the initiative to seek a change, has been learning swift grammar, or is not intuitive. Simple is to know the grammar can still not know how to use Swift to do the project, so today did not go to see the grammar of Swift, but to explore their own to do a swift simple application, Try to create a class and so on, which is written in the OC analogy. First we create an empty Applicatio project, language Select Swift, after creation is completed as follows.
When we open the Appdelegate.swift, we see this.
Here we will see a lot, although the syntax is not the same, but in fact, we can still understand the same as before we use OC created by the empty Application project is the same, such as we can change the background color or something, here we directly create an attempt to controller, Then let's create this trying controller to become our root view controller, if using OC maybe you say it's simple, but with Swift? This will be the case with Swift, which is for developers like me who have never used swift. The next step is to do the above, creating the same class as in OC, the code we set up should be like this.
Here we add a line of code, as we used to be the same as OC, before we will be written self.window.rootViewController = [[Firstviewcontroller alloc] init]; Here the principle is the same, but the code is not the same as the wording.
The next thing I want to do is create a class, declare properties, and add class methods and instance methods (that is, the-and +-number methods in OC). Take person as an example
Here is the person class we used to test, we use this class in Firstviewcontroller, and try to drop these methods.
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view!.backgroundColor = UIColor.blueColor()
var person = Person()
person.name = "Chris"
person.sex = "F"
person.age = 30
person.getUserName("Paul")
person.logPersonInfo()
Person.funcTest()
}
The results of printing are as follows.
What do we do if we want to create a "private" method before we write the OC code? It's actually very simple, so
I wrote a test method, a tune in the Viewdidload, the same way we did before (the self in Swift is the same as our OC usage, anyway, I feel it, and I need to know what the great gods are pointing out!)
Next we create a view to add to the root view, the code is as follows
var view = UIView(frame: CGRectMake(100, 100, 100, 100))
view.backgroundColor = UIColor.redColor()
self.view .addSubview(view)
In fact, the wording is the same, is the grammatical format has changed, do not know the API can go to see the document.
Two. Conclusion
Today did not go to see the grammar of swift, simple to do a project of the practice, also said is not the project bar, anyway is simply feel the swift to do the UI method, and OC principle is the same, not the same is the grammar habits, estimated that more can be used.
Swift starts learning _06 from scratch