oop - Add an extension/method to all objects in Swift -
in objective-c, objects can treated type id, , objects inherit nsobject. (blocks don't, that's exception.)
thus it's possible create objective-c category extends objective-c objects. (ignoring blocks)
in objective-c, created extension nsobject uses associated objects optionally attach dictionary nsobject. enabled me implement methods setassocvalue:forkey:
, assocvalueforkey:
makes possible attach key/value pair nsobject. useful in lots of circumstances.
it makes possible add stored properties category, example. write getter/setter uses associated value methods attach stored object, , away go.
it makes possible attach values existing system objects @ runtime. can hang data or blocks of code on buttons, or whatever need do.
i'd same thing in swift.
however, swift not have common base class objects objective-c does. anyobject
, any
protcols.
thus,
extension anyobject
won't compile.
i'm @ loss "attach" setassocvalue:forkey:
, assocvalueforkey:
methods in swift.
i create base class extension, defeats point of using extension. make base object objective-c nsobject, means objects have nsobjects, , swift objects not nsobjects default.
(btw, question applies both mac os , ios platforms)
no. you've pretty answered own question--swift objects don't have base class, , real way around inherit nsobject
.
Comments
Post a Comment