Swift and iosswift in ios development

Source: Internet
Author: User

Swift and iosswift in ios development

 In modern computer languages, object-oriented is a very important feature of iOS, and Swift language also provides object-oriented support. In addition, in the Swift language, not only classes have the object-oriented feature, but also struct and enumeration have the object-oriented feature..

 

  1. Classes and struct in Swift

The syntax for defining classes and struct in Swift is also very similar. We can use the class keyword to define the class. Next we define a Person class, which contains the age and name attributes respectively.

import UIKitclass JRPerson: NSObject {}

We found that the current class definition is very different from the previous OC definition, for the previous. m file and. the H file has disappeared. Now we define a class with only one file. swift file.

 

2. Add attributes

Here we do not need to set the set and get methods for defining properties as follows:

// Method 1: Give the default value var name: String = ""; var age: Int! = 0; // method 2 optional var name: String !; Var age: Int !;

 

3. Constructor

The constructor used by Swift is similar to java. We can initialize the constructor through the overload constructor. The system has a default constructor with no parameters, we can reload the constructor methods as needed.

// The Built-in System constructor override init () {super. init ();} // overload constructor. The init (name: String, age: Int) {super. init (); self. name = name; self. age = age;} // reload constructor. Use a Dictionary as the init (dic: Dictionary <String, String>) {super. init (); self. name = dic ["name"]; self. age = dic ["age"]?. ToInt ();}

 

4 object initialization:

// Use the default constructor to initialize var per0 = JRPerson (); // use the constructor with two parameters to initialize var per1 = JRPerson (name: "zhangsan", age: 11); // use the constructor with dictionary parameters to initialize var dic = ["name": "zhangsan", "age": "13"]; var per3 = JRPerson (dic: dic); 5. The method of the Object/*** prints the attributes of the current employee without parameters and returns no values */func show () {println ("name = \ (name) and age = \ (age)");}/*** prints that the attributes of the current person have parameters, the return value ==> arrow indicates the type of the return value. The return value is an integer */func show2 (num: Int, str: String, param: String) -> Int {for (var I = 0; I <num; I ++) {println (str);} return 10 ;}

 

6 methods

You only need to add the class in front of the class method as follows:

/*** Class method ** @ return <# return value description #> */class func show3 () {println ("hello ");}

 

7. method call 

// Use the default constructor to initialize var per0 = JRPerson (); // call the object method per0.show (); // call the class method JRPerson. show3 ();

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.