iOS SWIFT---initial iOS program

Source: Internet
Author: User

      Swift, Apple WWDC (Apple Developer Conference) release of the new development language, with objective-c * co-runs on mac OS and iOS platform application

It is believed that many people are interested in developing iOS, and since Apple has launched a simpler and easier-to-use swift, it is natural for anyone interested in iOS to plug in.

    • It's just a study diary, a record of my own learning process.
    • What's wrong? Welcome to correct, hope to progress together

The beginning of the iOS program, the steps to create the project I omitted to look at the structure of the iOS Project project


There are story board Main.storyvoard, the main delegation program Appdelegate.swift, controller Viewcontroller, some things we put in the future slowly familiar with, today we first to complete our first iOS program, a simple addition operation program.


1,main.storyboard Main Interface Story version

What is the story version, actually have the experience of Android development I think can be understood as layout on the line, and then we go through a series of drag control, we can see the effect


Here are two text field to get user input information and a label to display the result and a button to determine, some properties of the control can be adjusted in the story information panel, such as the font background



2,viewcontroller.swift Main Controller class

Actually, it's like activity ...

    • Any Viewcontroller class needs to inherit from Uiviewcontroller or its subclasses.
    • Need to overload the Viewdidload () function
    • Need to overload the Didreceivermemorywarning () function

  Call the    override Func Viewdidload () {        super.viewdidload ()        //Do any additional setup after when the controller's view class is loaded Loading the view, typically from a nib.    }    Call    override Func didreceivememorywarning () {        super.didreceivememorywarning ()        //Dispose when the system triggers a memory warning of any resources the can be recreated.    }
Next we drag our controls right-click into our controller class


We can also drag and drop events, such as button click events


Next we can handle the logic in the Click event of the button.

   @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 number"        }            }


Swift has the ability of type inference, that is, you do not have to deliberately indicate its type, the system will automatically infer, we only need to determine whether it is a variable var or constant Let, the above code we get the user input value to convert to int type for addition operation, But we also need to consider the other features of Swift if we cannot convert to int.

Optional type (optionals)

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

Extension string {///    If the String represents an integer, fits to an Int, returns//the    corresponding in Teger.  This accepts strings that match the regular//    expression "[-+]?[ 0-9]+ "only.    Func toint ()-Int?}
You can see that the return value is an int? That is, it returns a nil value when it is not converted to an int value.

So we can use the IF statement to determine the value after the forced solution to the seal! To get the returned int value, and finally run the effect.




This iOS program is the first small program we developed with Swift, and the more practical of swift syntax I will write it slowly, of course, Android I can't pull it down.


iOS SWIFT---initial iOS program

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.