iOS in Modern computer languages, object-oriented is a very important feature, and the Swift language also provides object-oriented support. Moreover, in swift language, not only the class has object-oriented characteristics, but also the structure and enumeration have object-oriented characteristics .
1. Classes and structs in Swift
The syntax for classes and struct-body definitions in Swift is very similar. We can define classes using the Class keyword, and below we define a person class that contains the age and name attributes, respectively.
Import UIKit class Jrperson:nsobject {}
We found that the definition of the class now differs greatly from the previous OC definition, and for the previous. m files and. h files have disappeared, and now we define a class with only one file. Swift file.
2 Adding properties
Here we define the properties without setting the set and Get methods as follows:
// mode one to default value var name:string=""; var age:int! =0; // mode two with optional type var name:string! ; var age:int!;
3 Construction methods
Swift uses a construction method similar to Java, we can initialize by overloading the constructor method, the system has a default constructor method is no parameters, we can according to their own needs to overload the construction method.
//System Self-band construction method Overrideinit () {super.init ();}//overloaded construction methods, with two attributes as parametersInit (name:string,age:int) {super.init (); Self.name=name; Self.age=Age ; }//overloaded construction method, using a dictionary as a parameterInit (dic:D ictionary<string,string>) {super.init (); Self.name=dic["name"]; Self.age=dic[" Age"]?. ToInt (); }
4 object initialization is as follows
//initialize with the default constructor methodvar per0=Jrperson (); //initialize with two-parameter construction methodvar Per1=jrperson (name:"Zhangsan", Age: One); //initialize with a constructor method with dictionary parametersvar dic=["name":"Zhangsan"," Age":" -"]; var per3=Jrperson (dic:dic);5methods of the object/** * Print out the current person's attributes no parameters, no return value*/func Show () {println ("name=\ (name) and age=\ (age)"); }/** * Print out the current person's attributes have parameters, there is a return value = = Arrow represents the type of return value, our return value at this time is shaping*/func show2 (num:int,str:string,param:string)-int{ for(Var i=0; i<num;i++) {println (str); } return Ten; }
Class 6 methods
class method only needs to be added in the previous class can be as follows
/* * * class Method * * @return < #return value description#> */ class func show3 () { println ("hello"); }
7 Invocation of the method
// Initialize with the default constructor method var per0=Jrperson (); // Calling Object Methods per0.show (); // Calling class methods JRPERSON.SHOW3 ();
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:
Swift Object-oriented in iOS development