Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

Swift init Array with capacity

How do I initialize an Array in swift with a specific capacity?

I've tried:

var grid = Array <Square> ()
grid.reserveCapacity(16)

but get the error

expected declaration 
question from:https://stackoverflow.com/questions/24124417/swift-init-array-with-capacity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Swift 3 / Swift 4 / Swift 5

var grid : [Square]?
grid?.reserveCapacity(16)

I believe it can be achieved in one line as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...