O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using shorthand declaration for collections

When declaring native Swift collection types, we should use the shorthand syntax, and, unless absolutely necessary, we should initialize the collection. The following example shows the preferred and non-preferred methods:

//Preferred Method 
var myDictionary: [String: String] = [:] 
var strArray: [String] = []  
var strOptional: String? 
 
// 
//Non-Preferred Method 
var myDictionary: Dictionary<String,String>  
var strArray: Array<String> 
var strOptional: Optional<String> 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required