[swift-users] edit array

Rien Rien at Balancingrock.nl
Wed Apr 26 09:27:28 CDT 2017


Because you are (trying) to edit a copy.

To edit the value in the array itself use:

array[index].number += 1

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift






> On 26 Apr 2017, at 16:15, J.E. Schotsman via swift-users <swift-users at swift.org> wrote:
> 
> Simple question: why can’t I edit a variable array with a functional method?
> 
> For example:
> 
> struct TestStruct
> 	{
> 	var number = 0
> 	}
> 
> var array = [TestStruct](repeatElement(TestStruct(), count: 2))
> array.forEach { $0.number += 1 }
> 
> Jan E.
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list