

{"id":120581,"date":"2024-01-05T18:00:48","date_gmt":"2024-01-05T12:30:48","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=120581"},"modified":"2024-01-05T18:19:52","modified_gmt":"2024-01-05T12:49:52","slug":"swift-structures","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/swift-structures\/","title":{"rendered":"Swift Structures with Examples"},"content":{"rendered":"<p>Structures are user-defined data types. It groups similar data into a single unit. It is also known as Structs. It can be used as an alternative for classes in some cases. In this article, we\u2019ll learn about its syntax and usage with relevant examples.<\/p>\n<h2>Swift Struct Syntax<\/h2>\n<p>To define a struct, we write the struct keyword followed by the name of the structure. We enclose its methods or properties within it using curly braces {}<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Struct structName{\r\n    property1\r\n    property2\r\n    ...\r\n      propertyN \r\n}<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct websiteInfo{\r\n    var name: string\r\n    var website: string\r\n}<\/pre>\n<h3>Instance of a Struct in Swift<\/h3>\n<p>We create an instance to use the structures. After creating the instances, the properties and methods can be accessed.<\/p>\n<p><strong>Syntax of creating an instance of struct.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let\/var exampleInstance = exampleStruct()<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct websiteInfo{\r\n    var name: String = \"DataFlair\"\r\n    var website: String = \"https:\/\/data-flair.training\/\"\r\n}\r\n\r\nvar myWebsite = websiteInfo()\r\nprint(myWebsite)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>websiteInfo(name: &#8220;DataFlair&#8221;, website: &#8220;https:\/\/data-flair.training\/&#8221;)<\/p>\n<h3>Memberwise Initializers<\/h3>\n<p>Instead of assigning the values to the properties of the struct during declaration, we can assign values while creating the instance of the struct. These are also called memberwise initializers.<\/p>\n<p><strong>Syntax of memberwise initializers:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let exampleInstance = exampleStruct(property1: val1, property2: val2, ..., propertyN:valN)<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct websiteInfo{\r\n    var name: String \r\n    var website: String\r\n}\r\n\r\nvar myWebsite = websiteInfo(name: \"DataFlair\", website:  \"https:\/\/data-flair.training\/\" )\r\nprint(myWebsite)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>websiteInfo(name: &#8220;DataFlair&#8221;, website: &#8220;https:\/\/data-flair.training\/&#8221;)<\/p>\n<h3>Accessing a Struct<\/h3>\n<p>We can access the properties of the strict using dot (.) notation.<\/p>\n<p><strong>Syntax for accessing strict properties.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let exampleInstance = exampleStruct(property1: val1)\r\nvar example = exampleInstance.property1<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct websiteInfo{\r\n    var name: String = \"DataFlair\"\r\n    var website: String = \"https:\/\/data-flair.training\/\"\r\n}\r\n\r\nvar myWebsite = websiteInfo()\r\nprint(\"Name of the website: \\(myWebsite.name)\")\r\nprint(\"Link of the website: \\(myWebsite.website)\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong>Name of the website:<\/strong> DataFlair<br \/>\n<strong>Link of the website:<\/strong> https:\/\/data-flair.training\/<\/p>\n<h3>Function Inside a struct<\/h3>\n<p>A structure can also include a method\/function within it. It might use the properties in its function. To access a function, we use the dot notation as we use to access the struct properties.<\/p>\n<p><strong>Syntax of a function inside a struct.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Struct structName{\r\n    property1\r\n    property2\r\n    Func functionName(){\r\n          \/\/ statements\r\n      }\r\n}<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct websiteInfo{\r\n    var name: String \r\n    var website: String \r\n    \r\n    func helloFunction(name: String){\r\n        print(\"Hello from \\(name)!\")\r\n    }\r\n}\r\n\r\nvar myWebsite = websiteInfo(name: \"DataFlair\", website:  \"https:\/\/data-flair.training\/\" )\r\n\r\nmyWebsite.helloFunction(name: myWebsite.name)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Hello from DataFlair!<\/p>\n<h3>Swift Structures: A Value Type<\/h3>\n<p>Structures are value types of data structures. This means that the values are copied while assigning them to instances, variables, constants or while passing through a function. Every time the value is passed around in the code, the values are copied. This is also known as the data is passed by value rather than reference.<\/p>\n<h3>Conclusion<\/h3>\n<p>Structures are user-defined custom data types. It helps us manage complex data in a simpler format. This article covers the definition and usage of swift structures. It shows how we can access its methods and properties. This also helps us understand its appropriate usage with the help of relevant examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Structures are user-defined data types. It groups similar data into a single unit. It is also known as Structs. It can be used as an alternative for classes in some cases. In this article,&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":120583,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27789],"tags":[28792,21771,28791,28287],"class_list":["post-120581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift-tutorials","tag-structures-in-swift","tag-swift","tag-swift-structures","tag-swift-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift Structures with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Swift Structures are user-defined custom data types. It helps us manage complex data. This article covers the definition and usage of structs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/swift-structures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Structures with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Swift Structures are user-defined custom data types. It helps us manage complex data. This article covers the definition and usage of structs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/swift-structures\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-05T12:30:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-05T12:49:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-structures.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift Structures with Examples - DataFlair","description":"Swift Structures are user-defined custom data types. It helps us manage complex data. This article covers the definition and usage of structs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/swift-structures\/","og_locale":"en_US","og_type":"article","og_title":"Swift Structures with Examples - DataFlair","og_description":"Swift Structures are user-defined custom data types. It helps us manage complex data. This article covers the definition and usage of structs.","og_url":"https:\/\/data-flair.training\/blogs\/swift-structures\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-01-05T12:30:48+00:00","article_modified_time":"2024-01-05T12:49:52+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-structures.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Swift Structures with Examples","datePublished":"2024-01-05T12:30:48+00:00","dateModified":"2024-01-05T12:49:52+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/"},"wordCount":366,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-structures.webp","keywords":["structures in swift","Swift","swift structures","swift tutorials"],"articleSection":["Swift Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/swift-structures\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/","url":"https:\/\/data-flair.training\/blogs\/swift-structures\/","name":"Swift Structures with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-structures.webp","datePublished":"2024-01-05T12:30:48+00:00","dateModified":"2024-01-05T12:49:52+00:00","description":"Swift Structures are user-defined custom data types. It helps us manage complex data. This article covers the definition and usage of structs.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/swift-structures\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-structures.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-structures.webp","width":1200,"height":628,"caption":"swift structures"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/swift-structures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Swift Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/swift-tutorials\/"},{"@type":"ListItem","position":3,"name":"Swift Structures with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=120581"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120581\/revisions"}],"predecessor-version":[{"id":132925,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120581\/revisions\/132925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/120583"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=120581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=120581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=120581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}