Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

The following code snippet results in a compile time error: struct IntStack { var items = [Int]() func add(x: Int) { items.append(x) // Compile time error here. } } Explain why a compile time error occurs. How can you fix it?

1 Answers  


Is swift memory safe?

0 Answers  


Which is the superclass of all the view controller objects?

0 Answers  


What is “defer”?

0 Answers  


What language is swift written?

0 Answers  


Why we use lazy in swift?

0 Answers  


What is atomic swift?

0 Answers  


What is the full meaning of swift?

0 Answers  


What is $0 and $1 in swift?

0 Answers  


What type of literals does swift language have?

0 Answers  


What is swift programming?

0 Answers  


What are type methods in swift?

0 Answers  


Categories