TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift Extension Methods: String, Int Extensions

Extension. A class or struct contains many useful funcs. But sometimes this is not enough. An additional method is needed. An extension method can solve this problem.
With the extension keyword, we add new methods to existing types like String or Int. A program might need to add 10 to a number. We can insert an addTen method to Int.
String extension. Let us begin with a String extension method. We use an "extension String" block. We introduce "middleChar() which" returns a character.

Self: The self-reference in an extension method accesses the instance we are calling the extension method on.

MiddleChar: This method loops forward from startIndex, and backward from endIndex, to return the middle Character from a String.

StringendIndex
Swift program that uses extension method extension String { func middleChar() -> Character { // Get middle char from a String. var a = self.startIndex var b = self.endIndex.predecessor() while true { if a >= b { return self[a] } a = a.successor() b = b.predecessor() } } } // Test our string extension method. let letters = "abc" let middle1 = letters.middleChar() print(middle1) let shirt = "shirt" let middle2 = shirt.middleChar() print(middle2) let leaf = "leaf" let middle3 = leaf.middleChar() print(middle3) Output b i a
Mutating, Int. An extension method can change the underlying object. We use the mutating keyword to indicate the "self" reference is being changed. This allows us to change an Int value.

AddTen: We implement this func as an Int extension. The Int value is changed when addTen() executes.

Note: Mutating methods sometimes make more sense. But often just creating a new instance and returning it is sufficient.

Swift program that uses extension, mutating func extension Int { mutating func addTen() { // Change value of self in mutating func. self += 10 } } var number = 200 // Test addTen method. number.addTen() print(number) number.addTen() print(number) Output 210 220
A review. Extension methods are powerful. They allow us to decorate existing types with additional, customized, specific methods. We compose programs that are clearer and easier to read.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf