Question

In: Computer Science

swift language declare a Swift array variable that can hold instances of any class type

swift language

declare a Swift array variable that can hold instances of any class type

Solutions

Expert Solution

Given, We need to declare the array variable in swift such that it can store instances of any class type.

Generally, Arrays are used to store the homogenous type of data. i.e; data of a particular class or type ca be stored. The same definition applies to arrays in swift also.

We can use the simple array declaration using the following syntax if and only if we know the type of instance to be stored and only that particular type is stored.

The syntax is as follows :

var arrayName = [instanceType]()

The syntax stores only that particular type of instance you need to store.

But, there is a small difference in swift that if we do not know the type of instance we need to store, we can use a particular way to store the data. This helps in storing instances of any class type.

There are 2 ways of doing it:

1. Using AnyObject.

2 Using Any.

The only difference that exist between the two is that AnyObject can represent instance of any class type whereas Any can represent an instance of any type including the function types.

We use them in the following way:

1. Using Any :

Source Code :

import Foundation
import Glibc

var arr = [Any]()
arr.append("hello")
arr.append(2)
arr.append(3.256)
print(arr[0])
print(arr[1])
print(arr[2])

Output :

hello
2
3.256

2. Using AnyObject :

Source Code :

import Foundation
import Glibc

class student {
var studname = "raja"
var mark = 98
var mark2 = 95
}

class teacher
{
var name = "john"
}

var arr = [AnyObject]()
let m = student()
let t = teacher()
arr.append(m)
arr.append(t)
print(arr[0])
print(arr[1])

Output :

main.student
main.teacher

Here, We can see that the two values in the array are the objects of the classes student and teacher respectively.

Simple Synatx :

Source Code :

import Foundation
import Glibc

class student
{
var studname = "raja"
var mark = 98
var mark2 = 95
}

var arr = [student]()
let s1 = student()
let s2 = student()
arr.append(s1)
arr.append(s2)
print(arr[0].studname)
print(arr[1].studname)

Output :

raja
raja

The screenshots are as follows :

Using Any :

Using AnyObject :

Simple Syntax :

I hope that this will give you all the information you needed regarding arrays in swift and how to store the instances of a class in an array.


Related Solutions

Java language Exercise #2: Design a Lotto class with one array instance variable to hold three...
Java language Exercise #2: Design a Lotto class with one array instance variable to hold three random integer values (from 1 to 9). Include a constructor that randomly populates the array for a lotto object. Also, include a method in the class to return the array. Use this class in the driver class (LottoTest.java) to simulate a simple lotto game in which the user chooses a number between 3 and 27. The user runs the lotto up to 5 times...
Activity 12: Array What is an Array? An array is a special variable, which can hold...
Activity 12: Array What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: var car1 = "Saab"; var car2 = "Volvo"; var car3 = "BMW"; However, what if you want to loop through the cars and find a specific one? And what if you had not...
A class variable is visible to and shared by all instances of a class. How would...
A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? Describe the difference between abstract classes and concrete classes, giving an example of each. Explain how data are encapsulated and information is hidden in Java? Explain the difference between a class and an interface in Java, using at least one example.
How does one declare a class variable in C#?
How does one declare a class variable in C#?
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class...
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class variables, map and guess that are two dimensional arrays of ints. Part B: In the constructor of the Player class, initialize the map and guess arrays with the appropriate size: map: 10 by 10 (10 outer arrays with 10 elements each) guess: 5 by 2 (5 outer arrays with 2 elements each) Step 2: Player.readPlayerSheet() Part A The readPlayerSheet method in the Player class...
This class will model a class variable to hold a mathematical function of up to 10 terms.
// Complete the implementation of the following class.// This class will model a class variable to hold a mathematical function of up to 10 terms.// The function will have one independent variable.// Terms that the function will model are:// Monomials of the form: 5.3x^0.5// Trigonometric terms sin(x), cos(x), tan(x) of the form: 1.2cos(3.0x)//public class Function { // PLEASE leave your class name as Function   // CLASS VARIABLES     // use any collection of variables as you see fit.     // CONSTRUCTORS   //...
Update the function playScratchOffs to do the following Declare an integer variable to store the type...
Update the function playScratchOffs to do the following Declare an integer variable to store the type of scratch off (i.e. type) Declare an integer variable to store the number of scratch off (i.e. count) Declare an integer variable for loop control (i.e. c) Declare a variable of data type struct OneDollar (i.e. oneSO) Declare a variable of data type struct TwoDollar (i.e. twoSO) Declare a variable of data type struct FiveDollar (i.e. fiveSO) Write a series of printf statements to...
Part A: Write a set of statements that declare a variable of type double called yourNum,...
Part A: Write a set of statements that declare a variable of type double called yourNum, then prompts the user to input a value which gets stored into that variable. Part B: Write a set of statements that displays the phrase "Even Number!" when the value in the variable count is an even number but displays the phrase “That is odd” otherwise. Declare and initialize all variables. Part C: Write the necessary code to swap (exchange) the values in two...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT