TheDeveloperBlog.com

Home | Contact Us

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

<< Back to SWIFT

Swift Subscript Example

Use the subscript keyword in a class to provide access based on arguments.
Subscript. Sometimes a class contains a store of data. It may have an array of animal names. With a subscript, we access those names with array-like syntax.
A simplification. A subscript uses property keywords (like get, set and newValue). It accepts one or more arguments that determine the value to be returned.
An example. This program uses a class with a subscript. The subscript internally just accesses four fields on the class. It uses no array as a backing store.

Subscript: This is part of the Example class. It receives two Int arguments. It returns a String.

Get: We use some logic to determine which of the four fields to return. This is not an ideal design. But it uses subscript syntax.

Set: Uses if-statements to determine which field to assign to the newValue. The newValue is a keyword that refers to the assignment value.

Swift program that uses subscript, class class Example { var value1: String = "" var value2: String = "" var value3: String = "" var value4: String = "" subscript(row: Int, column: Int) -> String { get { // Get field based on row and column. if row == 0 { if column == 0 { return value1 } else { return value2 } } else { if column == 0 { return value3 } else { return value4 } } } set { // Set field based on row and column. if row == 0 { if column == 0 { value1 = newValue } else { value2 = newValue } } else { if column == 0 { value3 = newValue } else { value4 = newValue } } } } } // Create our class and use the subscript. var ex = Example() ex[0, 0] = "cat" ex[0, 1] = "dog" ex[1, 0] = "bird" ex[1, 1] = "fish" // Read values from the fields. print(ex[0, 0]) print(ex[0, 1]) print(ex[1, 0]) print(ex[1, 1]) Output cat dog bird fish
Concepts. A subscript typically will access an array (including a 2D array) or a dictionary. But it can do anything. It can even validate arguments and do nothing or return a default value.2D Array
Structures, classes. A subscript may be used on either a structure or a class. It is a special method type that allows simpler access to internal data of the type.
A review. Subscripts provide array-like, intuitive access to class data. They are called in external places. With subscripts, we can validate access to data.
© 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