Struct Instances
A struct definition is simply a blueprint. We want to make an Associate in Nursing instance of it to use a struct. for instance,
struct Person {
name = " "
age = zero
}
// produce instance of struct
var person1 = Person()
Here, we've created an Associate in Nursing instance by writing the title of the structure Person accompanied by a revert value.
Now, we can use the person1 sample to access and modify the struct properties. for instance,
// modify the name property
person1.name = "Swift"
// access the age property
person1.age
Here, we've used the dot notation to access struct properties.
Example of Swift Access Struct Properties
//first define the structure
struct Person {
// then,define the two properties which is name and age
var name = ""
var age = 0
}
//after that create instance of Person
var person1 = Person()
// access properties and assign new values
person1.age = 20
person1.name = "Neha"
print("Name: \(person1.name) and Age: \( person1.age) ")
Output:
Name: Neha and Age: 20
In the above example, we have explained the struct named Person with two possessions: name and age.
We have also created an illustration of person1 of the struct Person.
Finally, we have accessed and modified the properties of an instance using .notation.
Example of Create Multiple Instances of Struct
// first define the structure
struct Student {
// then define the property
var studentID = 0
}
// instance of the Person
var student1 = Student()
// access properties and assign new values
student1.studentID = 111
print("Student ID: \(student1.studentID)")
// another instance of Person
var student2 = Student()
// access properties and assign new values
student2.studentID = 113
print("Student ID: \(student2.studentID)")
Output:
Student ID: 111
Student ID: 113
Swift Memberwise Initializer
Prior we tend to distribute a default value to a struct property.
struct Person {
var name = ""
}
We have made Associate in the default initializer.
var person1 = Person()
Nonetheless, if we do not assign a default value:
struct Person {
var name = String
}
We need to pass the value while creating an instance.
var person1 = Person(name = "Coder")
Best Usage Practices of Structures
Swift four language provides the practicality to outline structures as custom knowledge varieties for building the perform blocks. The instances of structure square measure elapsed its worth to the outlined blocks for more manipulations.
Need for having structures.
- To encapsulate easy knowledge values.
- Copy the encapsulated knowledge and its associated properties by 'values' instead of by 'references'.
Frequently Asked Questions
What is Swift Structure?
We use a struct or structure in swift to store variables of different data types. For example,
Suppose you want to store the address and age of a person. We can create two variables: address and age and store the values.
However, If you want to store the same information of multiple people.
In that case, creating variables for an individual might become difficult. To overcome this, you can create a struct that stores address and age. Now, this struct can be used by everyone.
Can you use Swift to call your own Objective-C code or a third-party library? If so, how?
When adding your first .swift file to your Xcode Project you will be prompted to let Xcode create a bridging header file. In that header file, you can import the Objective-C headers that you want to be visible to your Swift code. Then, all of those classes will be available to your Swift code without further imports. You can use your custom Objective-C code with the same Swift syntax you use with system classes.
What is the long run of Swift?
This is solely version one, Apple's intentions square measure clear that they'll be iterating on this language.
To get a plan of what could also be coming back down the pipeline, examine Karol Mazur's glorious outline of posts from Apple to the dev forums.
So make certain to report bugs and request features! there are heaps of areas to envision enhancements before version one is formally free.
Conclusion
From this article, we learned about the Swift structures. We learned how they are used. We also saw how to use swift instance. We looked at why we need swift and what is the future of swift.
We hope that this blog has helped you enhance your knowledge of Swift Structure and if you would like to learn more, check out our articles on Swift Tuples and Strings in Swiftt. Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.