Swift journey: 1. Overview-using Swift to implement a simple calculator

Source: Internet
Author: User

Swift journey: 1. Overview-using Swift to implement a simple calculator
Resources used for study: Stanford Open Course: iOS 8 Development Netease blank course class link: Stanford Open Course: IOS8 Development
Subtitle link on github: subtitle Baidu online storage resource download link: Baidu online storage
1. Detailed explanation of four layers of IOS programming ① cocoa touch layer this is our longest used layer, the longest used of course is the most basic control

② Media)
③ Core service layer
④ Core operating system layer

2. Create a Swift IOS project ① open Xcode and create a project as shown in the figure below

② Select Application and click Sigle View Application.

③ Enter the corresponding columns according. Value of companyidentifier + value of product name = bundle identifier in App ID
④ Set the saving path for the project:

⑤ Creating a new project will end, so easy:]
3. Build the page ① click Main. storyboard To Go To The Story page. We can see that there is a View Controller on the story version, which is the place where the interface is set.

② Drag a Label control to the Controller.
③ Adjust the Label size, content, and position. When adjusting the position and size, try to rely on the blue dotted line.



④ Select the simulator as IPhone6 and run the program.

⑤ No Label control is found in the simulator.
6. The Rotate Left in Hardware is used to Rotate the simulator and the Label is displayed.


4. autoLayout (Automatic Layout)

Select the Trailing Space to Controller Margin, which determines the distance between the right side of the Label and the right side of the Controller.

② Constraint between the Label top and the Controller TOP


③ Constraints between the left of the Label and the left of the Controller


④ Modify the warning in the Controller according to the following operations.



⑤ At this time, we can see that no matter how we change the simulator, the 0 position will remain unchanged.
3. Implement the corresponding logic by using code ① Xcode will automatically generate a ViewController class, which inherits from UIViewController. The two methods of the parent class are reloaded. We do not need to delete them now.

② Follow the instructions and click Show the Assistant editor to display the code and storyboard.

③ Drag the label control and go to the code editing box. A blue horizontal bar appears. Then, release the mouse.
④ In the pop-up dialog box, enter display in Name, and click Connet to indicate that the connection is successful.
⑤ Add the UIbutton control to the storyboard.

Connect UIbutton to the code area and release the mouse. The following dialog box is displayed. Then we set Connection to Action, indicating that this is a method. Enter appendDigit in Name. Then click Connect

Then we set the UIbutton attributes and location


6. Then we quickly deploy a simple calculator by copying and pasting it. (Copying not only copies the style of the control, but also associates the associated attributes and Methods)
7. Logic code:

Import UIKitclass ViewController: UIViewController {@ IBOutlet weak var display: UILabel! // Var: defines a variable type derived from var userIsInTheMiddleOfTypingANumber = false @ IBAction func appendDigit (sender: AnyObject) {// let is used to define a constant, constant type derived from the right // sender. currentTitle is used to obtain the title of the currently clicked button. let digit = sender. currentTitle !! // Println is used to output the string and \ () is used to contain the corresponding variable println ("digit = \ (digit)") // is used to determine whether the number is entered for the first time if (userIsInTheMiddleOfTypingANumber) {// if it is not the first time you enter a number, the number is spliced into the text display of the Label. text = display. text! + Digit} else {// if the number is entered for the first time, the text display of the Label is assigned directly. text = digit // if the number entered for the first time is not 0, change it to true if (digit! = "0") {userIsInTheMiddleOfTypingANumber = true }}}}
* Let is used to define a constant (const), which cannot be changed after being defined. Initialization is required. If Initialization is not performed, an error is returned. * Var is used to define variables and must be initialized. If it is not initialized, an error is returned. * The swift language supports type derivation. the type of the defined variable or constant is determined by the value on the right. * \ () Is used to include corresponding variables or constants in a string.
* Optional Type: This Type of value has two States: one is null (nil) and the other is null. That is, the Optional type allows the variable to have no value. If there is no initialization value for other types, an error is returned. When optional is worthwhile, the value type is? The value type on the left is determined. Yes! To obtain the corresponding type. Value of the optional type cannot be used for the right value !!!!
* Press and hold the option key and click the corresponding variable to view the document.



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.