ios - Strange Swift Protocol behaviour -


this question has answer here:

having trouble using swift protocol simplify uipageviewcontroller:

i have protocol

protocol pagable {     var pageindex: int? { set } } 

which have of uiviewcontrollers being presented uipageviewcontroller conform to.

then in uipageviewcontroller, this:

var vc = storyboardscene.challenges.acceptedviewcontroller() as! pagable    vc.pageindex = index return vc as? uiviewcontroller 

which works, want is:

var vc = storyboardscene.challenges.acceptedviewcontroller() (vc as? pagable)?.pageindex = index return vc 

and reason, whenever instead (which me feels same snippet 1), error on (vc as? pagable)?.pageindex = index saying "cannot assign immutable expression of type int?".

i'm thoroughly confused. love insight why type system doing me.

in

var vc = storyboardscene.challenges.acceptedviewcontroller() (vc as? pagable)?.pageindex = index 

vc variable, (vc as? pagable) immutable expression.

the solution declare "class-only protocol":

protocol pagable : class {     var pageindex: int? { set } } 

then compiler knows conforming types reference types, property can assigned if reference constant.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -