[IOS] Swift class and structure, storage attributes, computing attributes, functions and methods, ancillary scripts, etc., iosswift
I wrote 12 persons for review, but the number of times the same code needs to be typed on the keyboard is much less than that of the OC, thanks to the absence of semicolons in Swift, and missing brackets in OC.
I. Classes and struct
The two are not much different in Swift.
Classes and struct have many similarities: (point 2 and 3 do not exist in other languages)
1) You can define attributes, methods, and subscripts (struct can also define methods)
2) All can be initialized (through the constructor)
3) extensions and protocols can be used)
Many functions of analogy struct:
1) can inherit
2) Check the Class Object Type During Running
3) release resources
4) reference count allows a class instance to have multiple references
Class Person1 {var name = "jack"} struct Person2 {var age: Int} // 1.1 instantiate the class and structure. default constructor let p1 = Person1 () let p2 = Person2 (age: 10) // when using the default constructor, all attributes in the constructor must be initialized.
2. attributes (storage Attributes -- computing Attributes -- class attributes)
2.1 storage attribute: stores a constant or variable, similar to a member variable in Java.
Class Person3 {var name: String = "xuneng" // you need to manually initialize var age: Int = 10 let height = 175 lazy var p1: Person1 = Person1 () // latency, must be var} var p3 = Person3 () p3.name // use the dot syntax to access p3.age = 11 // set // delay lazy storage attributes: initialization is performed only when the attribute is changed, the advantage is that you do not need to waste space println (p3.p1) // This sentence is called before p1 initialization.
2.2 calculation attribute: Do not store values directly, but use get/set to perform operations on changes to other attribute values.
Get: The process of encapsulating values.
Set: set, used to encapsulate the process of setting a value
Class Person4 {var name: String = "jack" var jjLength: Int = 10 var age: Int {// only var get {return jjLength * 2} set (newAge) {jlength = newAge/2 }}var p4 = Person4 () p4.age = 10 // when age is set to 10, jjLength is changed to 5
2.2.1 Simple Writing of computing attributes
// The default newValue parameter indicates the newly assigned value class Person5 {// use newValue var jjLength: Int = 10 var age: int {// only var get {return jjLength * 2} set {jjLength = newValue/2} // read-only calculation attribute var height: int {get {return jjLength * 4} var height2: Int {// read-only can directly omit get return jjLength * 4} var p5 = Person5 () p5.height // p5.height = 10 // read-only objects cannot be assigned a value.
2.2.2 pay attention to the endless loop of computing attributes
The value of the calculated attribute is not fixed. Therefore, you can only use var instead of let to modify the attribute.
Calculation attributes cannot be assigned directly.
Class Person6 {// use newValue var jjjlength: Int = 10 // var height2: Int = 10 {// The value cannot be assigned directly, otherwise, the storage property // return jjLength * 4 //} var age: Int {get {return age // endless loop. age will call the get method} set {age = newValue // infinite loop. the set method will be called when age is set }}}
2.3 class attributes (or type attributes) are modified with the class keyword. class attributes can only be computed attributes.
A class attribute is a property similar to the static modification in java. A class has only one copy and multiple instance objects are shared. It can be called directly by class name.
class Person7{ class var name:String{ return "xuneng" }}Person7.name
2.4 property monitor: two methods that can be used to monitor property changes: willSet and didSet
Because the get and set methods are available for computing attributes, the monitor is not very useful for computing attributes.
When the value is initialized, willSet and didSet are not called.
WillSet, didSet, and set cannot coexist.
Class Person8 {var name: String = "xuneng" {willSet {println ("New Value: \ (newValue )") // newValue new value} didSet {println ("New Value: \ (oldValue)") // oldValue indicates old value }}var p8 = Person8 () p8.name = "jack" // This call
Iii. Functions and Methods
3.1 object Method
A difference between a method and a function in a class: the first parameter of a method is followed by an external parameter. The name is the parameter name.
Class Person9 {func sum (num1: Int, num2: Int) {println (num1 + num2) }}var p9 = Person9 () p9.sum (10, num2: 10) // after the first parameter, all parameters are external parameters. equivalent to adding #
3.2 class method. Use class to modify
class Person10{ class func sum(num1:Int , num2:Int){ println(num1 + num2) }}Person10.sum(10, num2: 10)
3.3 self and super, same as OC
Class Person11 {func sum (num1: Int, num2: Int) {self. fun () // object method call object method, class method call class method} func fun () {println ("hello ")}}
Iv. subscripts)
Subscripts, some of which are subscripts and new things.
It can be defined in the class/struct/enumeration for quick access.
// Format: set/get/* subscript (index: Int)-> Int {get {} set {}}*/
Struct Person12 {// many officially used struct let jjLength: Int // here, subscript (index: Int) can be not initialized) -> Int {return jjLength * index} var p12 = Person12 (jjLength: 10) p12 [3] // directly access the object like an array. based on jj length, index is 3 and value is 30
Refer:
The Swift Programming Language
Apple Dev Center
Reprinted please indicate the source: http://blog.csdn.net/xn4545945
What is a structured data storage type?
Structured Data Storage stores data according to a certain structure, such as the NSDictionary of ios. A Jian corresponds to a value. To facilitate data operations,
Generally there are 3 types
Key Value archive (one key corresponds to one value, and the key value)
Attribute list (such as xml file storage)
Database storage (direct database storage)
What are the benefits of the memory management mode for programmers to write programs?
Minimum requirements ......
Write programs