Ios swift-the first iOS Program

Source: Internet
Author: User

Ios swift-the first iOS Program

Swift, a new development language released by Apple at the 2014 WWDC (Apple Developer Conference), can run with Objective-C * on Mac OS and ios platforms, it is used to build applications based on the Apple platform.

I believe many people are very interested in ios development. Since Apple launched swift, Which is simpler and easier to use, it will naturally be a plug-in for anyone interested in ios.

This is just a learning diary, recording my learning process If you have any mistakes, please correct them and hope to make progress together.

At the beginning of the ios program, the steps for creating a project are omitted. Let's take a look at the structure of the ios project.


There is a storyboard Main. storyvoard, Master delegate AppDelegate. swift, controller ViewController, and some things we will be familiar with later. Today we will first complete our first iOS program, a simple addition computing program. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + Environment + a5ysrCsOY8L3A + Environment/dK7z7XB0LXEzc/Xp7/Environment + Environment = "http://www.2cto.com/uploadfile/Collfiles/20150212/2015021208582666.png" alt = "\">

These are the two Text fields used to obtain user input information, a label used to display the results, and a button used to determine the results, some Properties of the control can be adjusted in the Story information panel, such as the font background



2. ViewController. swift master controller class

Actually, it's like activity...

Any ViewController class must be inherited from UIViewController or its subclass. to overload the viewDidLoad () function, the didReceiverMemoryWarning () function must be reloaded.

// When the Controller's View class is loaded, override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view, typically from a nib .} // call override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}
Next, right-click our control and drag it to our controller class.


We can also drag events, such as button click events.


Next, we can process the logic in the button click event.

@ IBAction func addResult (sender: AnyObject) {var v1 = value1.text. toInt () var v2 = value2.text. toInt () if v1! = Nil & v2! = Nil {var countResult = v1! + V2! Result. text = String (countResult)} else {result. text = "Unknown error, only numbers can be calculated "}}


Swift has the ability to deduce types. That is to say, you do not need to indicate its type. The system will automatically infer it. We only need to determine whether it is a variable var or a constant let, in the above Code, we get the value entered by the user and convert it to the int type for addition operations. However, we also need to consider the case where the value cannot be converted to the int type, at this time, we will mention another feature of swift.

Optional type (optionals)

Let's take a look at the return value of the toInt method.

extension String {    /// If the string represents an integer that fits into an Int, returns    /// the corresponding integer.  This accepts strings that match the regular    /// expression "[-+]?[0-9]+" only.    func toInt() -> Int?}
The returned value is an Int? That is to say, an nil value is returned when it cannot be converted to an int value.

Therefore, we can use the if statement to determine whether a value exists and forcibly unseal it! To obtain the returned int value and the final running effect.

I cannot pull down Android either.


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.