Reputation: 23
I'm kinda new to programming, so this maybe a basic question. I'm trying to work with nested arrays in Swift.
@IBAction func testFuncTrigger(sender: UIButton) {
var tempTestArray = [];
tempTestArray = [["cos",["x"]],["sin",["cos",["x"]]],"5*x"];
tempTestArray[1] = "8*x";
tempTestArray[1] = ["8*x"];
tempTestArray[1] = ["e^",["sin",["x"]]];
}
gives the error "cannot assign to the result of this expression".
Also trying to put
@IBAction func testFuncTrigger(sender: UIButton) {
var tempTestArray = [];
tempTestArray = [["cos",["x"]],["sin",["cos",["x"]]],"5*x"];
tempTestArray[1] += "8*x";
tempTestArray[1] += ["8*x"];
tempTestArray[1] += ["e^",["sin",["x"]]];
}
gives the errors
Binary operator "+=" cannot be applied to operands of type 'AnyObject' and 'String'
and
Binary operator "+=" cannot be applied to operands of type 'AnyObject' and '[String]'
and
Binary operator "+=" cannot be applied to operands of type 'AnyObject' and '[NSObject]'
respectively.
Is there anyway to work around this?
Upvotes: 1
Views: 139
Reputation: 71854
If you declare an array like this:
var tempTestArray = []
The type of this array will be NSArray
because you doesn't define any type to your array. And you can not add any element into NSArray
that way.
Give a type to your tempTestArray
to AnyObject
like this:
var tempTestArray = [AnyObject]()
And it will work fine.
Upvotes: 1