ObjectiveC Code:
CCButton *mNumTiles[10];
I Tried this Swift code but Crashing
var mNumTiles : [CCButton]!
mNumTiles[0] = CCButton.buttonWithTitle(""
How to declare array of custom Class object in Swift ?
You've declared an array but haven't initialized one.
You can do it like this:
var mNumTiles = [CCButton]()
mNumTiles.append(CCButton(title: ""))
Note that you don't have to declare the type of mNumTiles; Swift will infer it from the initialization ([CCButton]())
: to =.Your variable declaration is incorrect, because the variable is not initialized (nil). To do that, you have to construct an array like this
var mNumTiles : [CCButton]! = [CCButton](count: 10, repeatedValue: nil)
Another way to do it would be to initialize an empty array and use append to add the button:
var mNumTiles : [CCButton]! = []
mNumTiles.append(CCButton....)
You should make sure to read this if you want to learn more about arrays in Swift:
NSArraybut a C-style array.