randomor
randomor

Reputation: 5673

"Type of expression is ambiguous without more context" when creating array of struct using an range in Swift 5

I'm trying to create an array of a simple struct using a range. What's the proper way to do it?

struct Stock {
   var id = UUID()
   var name: String = ["MSFT", "AAPL", "TSLA", "GOOG", "AMZN", "FB"].randomElement()!
}
//Generates the error: "Type of expression is ambiguous without more context"
var companies = (1...100).map { Stock() } 

I'm on Swift 5.2 and Xcode 12.

Upvotes: 0

Views: 108

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

You need to add _ in as you don't use $0 inside the block

var companies = (1...100).map { _ in Stock() } 

Upvotes: 1

Related Questions