original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
You can extend calculated properties on the original type, including instance calculation properties and static computed properties. The definition of a computed property is added, as is the definition of a normal computed property.
Example Compute Properties Examples: In order to reduce traffic when programming on the network, the information returned from the server is not described, but is encoded, and then the encoding is converted locally to descriptive information. The following INT type extensions are defined for this purpose :
extension int { / /define Extended var errormessage : string { //read-only computed properties of type int var errorstr = "" switch (self) { case -7: errorstr = " No data. " case -6: errorStr = "date is not entered. " case -5: errorStr = "content is not entered. " case -4: errorstr = "id not entered. " case -3: errorStr = "Failed to access the data. " case -2: errorStr = "Your account can insert up to 10 data. " case -1: errorStr = "User does not exist, please register to http://51work6.com. " default: errorStr = "" } return errorstr }}let message = ( -7) .errorMessage //Gets the description information for the 7 encoding print ("Error code : -7 , error message : \ ( Message) ")
Note the entire -7 including the minus sign is a complete instance, so calling its properties requires -7 enclosed in parentheses as a whole. However, if you are 7 , you do not need parentheses.
Let's look at a static Property Example:
struct account { //defining the account structure body var amount : double = 0.0 // Account amount var owner : String = "" //Account name} extension account { //define an extended static of the account structure body static var interestrate : double { //rates return0.0668 }} print ( account.interestrate) //Print output InterestRate properties
Print Output InterestRate property, accessed in the same way as other static computed properties, by the "type name" Plus " . to access the static computed properties.
Extension methods
You can extend methods on the original type, including instance methods and static methods. The definition of these additions is the same as for the normal method.
Let's look at an example below:
extension double {//defines the extension static var interestrate  of the Double type;: double = 0.0668//interest Rate func interestby1 () -> Double { return self * double.interestrate //static property interest rate } mutating func interestby2 () { //defines an instance method interestby2 self = self * Double.interestRate } static func interestby3 (amount : Double) - > double { //defining a static method Interestby3 return interestrate * amount //return value is calculated interest result }} let interest1 = (10_000.00). InterestBy1 () //call interestBy1 method to calculate interest print ("Interest 1 :\ (Interest1)" ) var interest2 = 10_000.00 // Call the InterestBy2 method to calculate the interest Interest2.interestby2 () print ("Interest 2 :\ (INTEREST2)") var interest3 = double.interestby3 (10_000.00) //calls the InterestBy3 method to calculate interest print ("Interest 3 :\ ( INTEREST3) ")
Code Self = self *double.interestrate To assign the result directly to the current instance Self . Assigning self to self in structs and enum types has a compile error, and you need to precede the method with the mutating keyword, which indicates that this is a mutation method.
call method calculates interest, invokes its instance 10_ 000.00 interest1
call method to calculate interest, we cannot use 10_ 000.00 instance calls, but requires a double interest2 interestby2 is the mutation method that directly alters the variable interest2 interest2.interestby2 () After the statement call is complete, the variable interest2 The value of
called InterestBy3 method calculates the interest, which is a static method that calls it to "type name . "The way that is" Double. "in the way called.
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "title=" 00.png "src=" http://s4.51cto.com/wyfs02/M02/7C/D6/wKioL1bZFcvAO2hFAAAs2MBEZnc814.png "alt=" Wkiol1bzfcvao2hfaaas2mbeznc814.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1747538
Learn notes from scratch (Day 50)--extend calculated properties, methods