To create a struct in Swift, you can define it using the struct
keyword. Here's an example:
main.swift56 chars5 lines
In this example, we've created a Person
struct, which has two properties: name
(a String
) and age
(an Int
).
You can then create an instance of the Person
struct like this:
main.swift49 chars2 lines
This creates a new Person
instance with the given name
and age
values.
Structs are a fundamental data type in Swift, and are commonly used to represent objects in object-oriented programming. They are value types, meaning that when you create a new instance of a struct, it is copied to a new location in memory.
gistlibby LogSnag