Consider the following code:
var array1 = [1, 2, 3, 4, 5]
var array2 = array1
array2.append(6)
var len = array1.count
What’s the value of the len variable, and why?
Answer Posted / iosraj
The len variable is equal to 5, meaning that array1 has 5 elements, whereas array2 has 6 elements:
array1 = [1, 2, 3, 4, 5]
array2 = [1, 2, 3, 4, 5, 6]
When array1 is assigned to array2, a copy of array1 is actually created and assigned.
The reason is that swift arrays are value types (implemented as structs) and not reference types (i.e. classes). When a value type is assigned to a variable, passed as argument to a function or method, or otherwise moved around, a copy of it is actually created and assigned or passed. Note that swift dictionaries are also value types, implemented as structs.
Value types in swift are:
structs (incl. arrays and dictionaries)
enumerations
basic data types (boolean, integer, float, etc.)
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is dynamic member lookup swift?
What is static function in swift?
What is extension in swift?
Can any be nil swift?
What language is swift most similar to?
Is swift an object-oriented programming language?
Explain some design patterns which we normally use during the app development.
What are the new feature in swift 4.0?
How do you implement delegates in swift?
What are the control transfer statements that are used in ios swift?
What is the characteristics of switch in swift?
What is swift property?
What is argument label in swift?
What is @objc in swift?
Explain the difference between let and var in swift programming?