xcode - '+=' cannot be applied to two [AnyObject] operands -
i have following code try , create array of constraints add view:
let views = ["button": button] let metrics = ["margin": 16] var constraints: [anyobject] = [] constraints += nslayoutconstraint.constraintswithvisualformat("|-margin-[button]-margin-|", options: 0, metrics: metrics, views: views)
from understand swift arrays, should able '+=' them join two, error:
"binary operator '+=' cannot applied 2 [anyobject] operands"
what's wrong code?
it's not because of operator. it's because passing in int supposed pass nslayoutformatoptions
enum type.
if pass in 1 of nslayoutformatoptions
enum options
parameter, error go away:
constraints += nslayoutconstraint.constraintswithvisualformat("|-margin-[button]-margin-|", options: .alignallleft, metrics: metrics, views: views)
or initialize nslayoutformatoptions
int value want use, this:
nslayoutformatoptions(rawvalue: 0)
0 have worked in objective-c, need use actual enum value in swift. swift errors still misleading in many cases, one.
hope helps.
Comments
Post a Comment