multithreading - How dangerous is it to use pointer-style assignment versus setter-methods in Objective-C? -
lets have simple class following:
@interface { // @public int var; } // @property(some_property) int var; @end when want access variable var, have options. if create var public, can this:
a objecta = [ [ alloc ] init ]; nslog( @"%d", objecta->var ); objecta->var = somenumber; if create property instead, i'll have more this:
a objecta = [ [ alloc ] init ]; nslog( @"%d", objecta.var ); // dot-syntax nslog( @"%d", [ objecta var ] ); // get-syntax [ objecta setvar: somenumber ]; i've tried both , work fine question how unsafe utilize old-style pointer notation access variables within of object? have worry things later on should take care of standardizing accessing of methods? or can away doing want long works?
i'll throw own 2 cents in since comment of mine on question started similar give-and-take few days ago.
you should always utilize accessor methods set , object's properties outside of implementation of object's class. should almost utilize accessor methods access class's properties within implementation of said class.
here list of reasons why:
encapsulation. class may expose property outside world looks other property, internally not backed corresponding ivar. perhaps transformation of another, internal property. more importantly, implementation may alter sometime downwards line. 1 of major reasons encapsulation in oo code internal implementation of class can changed without requiring external users of class create changes. (i made such alter -- ivar different backing method -- in important, , old class morning. if bunch of other classes, or methods in same class doing direct ivar access, have had alter lot more code did.)
memory management. isn't big of deal arc, since (mostly, see below) handled either way, manual memory management, accessor method property take care of retaining , releasing underlying object. keeping code in 1 place makes much lower chance of memory management mistakes (leaks , overreleases), easier read, easier modify, less verbose code. arc enabled, properties declared using re-create behavior rely on setter method perform copy, , bypass if direct ivar access.
custom behavior on set. part of encapsulation. it's mutual class want beyond setting ivar new value when corresponding setter called. 1 common, simple illustration nsview subclass phone call [self setneedsdisplay] when property affects appearance changed. if don't phone call setter, instead setting ivar directly, bypass behavior altogether. again, might think it's fine long know setter doesn't need such thing, requirements change, , using setter start, create alter downwards line much easier.
custom behavior on get/lazy instantiation. 1 of mutual reasons lazy instantiation. implement getter method property checks see if underlying ivar nil, , if is, first initializes ivar before returning it. next time it's called, ivar set initialization won't happen again. easy way defer creation of expensive (cpu intensive and/or memory intensive) objects until , if needed. example, it's done performance optimization improve launch times, perfect illustration of encapsulation allowing simple improvements class's implementation without breaking external code's existing utilize of class.
kvo compliance. objective-c provides mechanism called key value observing allows 1 object request notification time given property of object changed. if utilize properly named accessors, or synthesized @properties, back upwards kvo automatically. however, kvo work, accessor methods have called. if alter ivar directly, observers of ivar's corresponding property not notified. whether you're setting object's property or property on self, don't know if observers registered changes on property , you're short circuiting beingness notified of change.
readability. doesn't apply objecta->var example, it's more applicable direct ivar access in class's own implementation (var = ...), self-> implied/inserted compiler. problem that, particularly in long method, may see assignment statement, , not know @ glance if variable beingness assigned local current scope, or instance variable. can alleviated naming conventions prefixing ivars underscore, hungarian notation, etc., still objective-c conventions mean it's best utilize self.var = ... or [self setvar:...] (which same semantically, way).
why not? there don't utilize accessor methods, can't think of reasons why. it's not much faster type, prefixing variable name 'self.' doesn't take much typing. there's performance penalty involved in calling accessor since add together objc message send. however, penalty small, , of course, premature optimization bad thing. it's very rare overhead of accessor method phone call plenty impact application performance.
remember used 'almost' qualifier regarding using direct ivar access within class's implementation. if implement custom accessor methods (as opposed @synthesizing property), have straight access ivar within accessors' implementations, of course. also, there disagree, it's thought (and apple's recommendation) straight set ivars within class's initializer (-init) method(s). reason may want avoid setter side effects while class not initialized. example, don't want kvo observers notified of alter when may find notifying object in inconsistent/partially initialized state.
objective-c multithreading pointers methods synthesize
No comments:
Post a Comment