Re: Is it possible for a subclass to set the ivar of its base class?

2016-10-16 Thread OC
Patrick,

On 24. 9. 2016, at 17:11, 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. ... Is there a way I can get access to 
> the _wut ivar in the base class

Note please that this kind of design is conceptually wrong and extremely 
error-prone, as Manoah did point out: *never* do this, unless *very* necessary 
and no cleaner way out.

If indeed *extremely* necessary, and if you *really* have no cleaner way out, 
you can do that, exploiting ObjC runtime, essentially like this:

===
43 /tmp> 
#import 

@interface Foo:NSObject
-(void)setFoo:(NSInteger)foo;
-(void)showFoo;
@end

@interface Bar:Foo // could be a category just as well, etc.
-(void)dirtyHackSetFooDirectly:(NSInteger)foo;
@end

int main(int ac,char **av) {
 id foo=[Bar new]; // if we used a category, subclass would not be needed
 [foo dirtyHackSetFooDirectly:666];
 [foo showFoo];
 return 0;
}

@implementation Foo {
  NSInteger _foo;
}
-(void)setFoo:(NSInteger)foo {
  _foo=foo;
  NSLog(@"unwanted side-effect");
}
-(void)showFoo {
  NSLog(@"foo is %ld",(long)_foo);
}
@end

@implementation Bar
-(void)dirtyHackSetFooDirectly:(NSInteger)foo {
  object_setInstanceVariable(self,"_foo",(void*)foo);
}
@end
44 /tmp> cc -Wall -framework Foundation q.m && ./a.out
2016-10-16 17:53:21.363 a.out[57747:707] foo is 666
45 /tmp> 
===

All the best,
OC


 ___
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

Re: Is it possible for a subclass to set the ivar of its base class?

2016-10-15 Thread Manoah F. Adams


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
 

For PGP/GPG key usage and tools, see 
===






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

Re: Is it possible for a subclass to set the ivar of its base class?

2016-09-24 Thread Patrick J. Collins
> You can’t add a method (setOtherWut) to the base class in the subproject? 
> That would be easier than mucking with the run-time.
Yeah I can..  I just was trying to avoid modifying someone else's
code...

If that's the way to do it then I guess that's what I will have to do.
That just seems highly unfortunate.

Patrick J. Collins
http://collinatorstudios.com
 ___
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


Re: Is it possible for a subclass to set the ivar of its base class?

2016-09-24 Thread Quincey Morris
On Sep 24, 2016, at 12:15 , Patrick J. Collins  
wrote:
> 
> There's no way to do this without duplicating tons of their code?

You can’t add a method (setOtherWut) to the base class in the subproject? That 
would be easier than mucking with the run-time. ___
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

Re: Is it possible for a subclass to set the ivar of its base class?

2016-09-24 Thread Patrick J. Collins
> could just subclass as make my own custom setter.  So I can call both
Oops sorry-- subclass *AND* make my own custom setter.

Patrick J. Collins
http://collinatorstudios.com
 ___
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

Re: Is it possible for a subclass to set the ivar of its base class?

2016-09-24 Thread Patrick J. Collins
> How do you know this *is* the implementation in the base class? How do you 
> know there *is* a private variable called “_wut”, or its type, or what value 
> it actually contains, or if there are any other
> variables that must be set consistently whenever the value of the “wut” 
> property changes?
>
> I don’t see how you can safely make your change without seeing the source 
> code of the base class, and it you have access to the source code you’d be 
> better off making a custom library.

I do have access to the base class-- the library is a sub-project within
my project. I just simply want the option to be able to prevent a single
side effect that happens in their setter method, so I was hoping I
could just subclass as make my own custom setter.  So I can call both
theirs and mine when appropriate.

There's no way to do this without duplicating tons of their code?

Patrick J. Collins
http://collinatorstudios.com
 ___
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


Re: Is it possible for a subclass to set the ivar of its base class?

2016-09-24 Thread Quincey Morris
On Sep 24, 2016, at 08:11 , Patrick J. Collins  
wrote:
> 
>@implementation Lol {
>int _wut;
>}
> 
>-(void)setWut:(int)wut {
>_wut = wut;
>// side effects I do not want.
>}

How do you know this *is* the implementation in the base class? How do you know 
there *is* a private variable called “_wut”, or its type, or what value it 
actually contains, or if there are any other variables that must be set 
consistently whenever the value of the “wut” property changes?

I don’t see how you can safely make your change without seeing the source code 
of the base class, and it you have access to the source code you’d be better 
off making a custom library.

 ___
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

Re: Is it possible for a subclass to set the ivar of its base class?

2016-09-24 Thread Jens Alfke

> On Sep 24, 2016, at 8:11 AM, Patrick J. Collins 
>  wrote:
> 
> When I run this, I get 0 instead of 123.  

Right; you’ve created a new, different ivar in the subclass, which just happens 
to have the same name.

> Is there a way I can get access to the _wut ivar in the base class

Yes, by using the power of . This is a super-powerful API that 
lets you open up and mess with classes like Legos. You can find the ivars and 
methods, modify variables, even create and remove methods and create new 
classes.

Looks like the function you’ll need is object_setInstanceVariable.

Obviously this great power should be used responsibly. I’d only recommend doing 
this as a last resort, if there’s no way to modify the library yourself. Also, 
if it's a system library, be careful because the library’s implementation could 
change in any future OS release and invalidate your hack, breaking your app.

—Jens ___
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