On Sep 24, 2016, at 08:11:00, Patrick J. Collins wrote:

I am trying to change the behavior of a library by subclassing one of the files, but am finding this is not working because I can't access (change) one
of the internal instance variables.

To illustrate my problem:

 Rofl *rofl = [[Rofl alloc] init];
 [rofl otherSetWut:123];
 [rofl speak];

 ----

   @implementation Lol {
       int _wut;
   }

   -(void)setWut:(int)wut {
       _wut = wut;
       // side effects I do not want.
   }

   -(void)speak {
       NSLog(@"so wut up? %i", _wut);
   }

   @end

 ----

   @implementation Rofl {
       int _wut;
   }

   -(void)otherSetWut:(int)wut {
       _wut = wut;
   }

   @end

 ----


When I run this, I get 0 instead of 123. Is there a way I can get access to the _wut ivar in the base class so that I will
How can I make it so that I will get the desired 123 output?

Thanks!

Patrick J. Collins
http://collinatorstudios.com




For one thing, I would avoid knowingly shadowing an iVar, as I don't see a benefit, and indeed, as I see it, it is preventing you from reaching the one the superclass is using. More importantly, if the superclass is indeed providing unwanted side- effects, then there is likely a design issue with that class -- in so far as the setter is doing too much.
They should be using a separate method for actual operations.

Further, bypassing the setter, when the rest of the superclass uses it (which is likely, though not necessarily true) will cause the super- class's code to behave improperly, so before using the runtime to directly modify it, check the bass class code to make sure it is not internally using the setter.





Manoah F. Adams
mhfad...@federaladamsfamily.com
federaladamsfamily.com/manoah

This message is signed with a certificate and-or PGP key.
Disregard any unverified messages from this address.
Email Certificates for personal use can be obtained from Comodo at
<https://secure.comodo.com/products/frontpage?area=SecureEmailCertificate >
For PGP/GPG key usage and tools, see <www.gnupg.org>
===========




Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (Objc-language@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to