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?



Consider the following code: var array1 = [1, 2, 3, 4, 5] var array2 = array1 array2.append..

Answer / 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

More Apple iOS Swift Interview Questions

How you define variables in swift language?

0 Answers  


Why swift is the best language?

0 Answers  


Explain any three-shift pattern matching techniques?

0 Answers  


What is nil-coalescing operator?

0 Answers  


How to post an http request with a json body in swift?

0 Answers  






What are the tools that are required to develop ios applications?

0 Answers  


In swift what is use of backticks while declaring a variable?

0 Answers  


What is differences in swift 1.x, 2.x, 3.x, 4.x?

0 Answers  


Is swift a good language?

0 Answers  


What is an attribute in swift?

0 Answers  


How to declare an empty dictionary in swift?

0 Answers  


What is the difference between swift and objective c?

0 Answers  


Categories