TheDeveloperBlog.com

Home | Contact Us

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

<< Back to SWIFT

Swift Struct (Class Versus Struct)

Use a struct. Review the difference between structs and classes.
Struct. A struct is a value type. Its data is stored directly in the variable's memory. This gives performance advantages, but also limits the type.
In Swift, structs are less often used in custom code than classes. For small units of data (like positions, sizes) structs are effective. They are copied when passed as arguments.
Struct versus class. Let us explore a key difference between structs and classes. When we create a struct, it is a value type. When we pass a struct to a func, it is copied.

Here: We create a tiny class (TestClass) and a tiny struct (TestStruct). We pass them both to methods, as arguments.

Class: The class reference is copied, but not the data of the class. So we can change the class's inner data.

Struct: A struct can be passed to a func, but its data cannot be modified in the func. It is a value, not a reference.

Swift program that uses struct, class class TestClass { var code: Int = 0 } struct TestStruct { var code: Int = 0 } func increment(t: TestClass) { // The class instance is shared, so we can modify the memory. t.code++ } func increment(t: TestStruct) { // The struct is copied, so cannot be modified in this func. // A new struct must be created. } // Create a class instance and modify it in a func. var y = TestClass() y.code = 1 increment(y) print(y.code) // Create a struct instance, which cannot be modified. var x = TestStruct() x.code = 1 increment(x) print(x.code) Output 2 1
Selecting structs. When should we use structs instead of classes? For small units of data, where the fields will not change often after creation, structs are a good choice.

However: For most custom types, classes are better. Structs have both performance advantages and negatives.

Performance: When we pass a struct to a func, all its data is copied. This can be faster (if the struct is small) or slower (if it is big).

A review. Structs have limitations. They are copied by value, so changes are disallowed or not reflected in the original variable. But structs can reduce allocations.
© 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