DawnGuardian
DawnGuardian

Reputation: 23

swift cannot assign to the result of this expression

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

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

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

Related Questions