How to set default value for all array content while init in Swift?

0 votes
109 views
asked Mar 3, 2015 in Swift by kalpana (3,995 points)
Hi All

  How to set default value all array content while init in Swift?

1 Answer

0 votes
answered Mar 3, 2015 by john (4,220 points)

Hi Kalpana

   Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to a provided default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue):

  • var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
  • // threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]

 

...