Prevent child class methods from firing? - objective-c

I am sure this question has been asked before, but I searched and couldn't find it so I apologize in advance for duplicating content here on SO.
That being said: In Objective-C, in an overridden method you can call the parent class method using something like [super methodName]
but how do prevent the rest of the code from executing in the child class from the parent? It could because it's Friday, but I stared at my monitor for a few minutes and couldn't get past it in my head.
Example (in child class):
- (void)methodName
{
[super methodName];
//Everything below this line shouldn't execute if tell it not to from the parent
NSString *aString = #"This should never be called.";
}
Help me out! I know there's a simple solution, but my brain just isn't picking it up today...

You could maybe have another function returning True/False which you use to decide if you wanna proceed with the remaining code in the child. This 'control' function can depend on a variable set in the parent class
Having said that, it sounds like an interesting requirement to me. Maybe you need to take another look at your class design and hierarchy.

you could test the object type and determine if you do or don't want to run additional code for example:
if (![myObject isKindOfClass:[MyChildObject class]]
{
//run only superclass code
}
As I write this, I can't help but wonder if you don't have some issue with how you setup your classes though. If your design is spot on, you shouldn't have to go through these kinds of contortions.

Related

How to change UI elements from a class in Kotlin?

So the situation is that I have a bluetooth class where I get some values, I want to display those values inside the UI, but for some reason I can't figure out how to do it. In swift I would do it with an observable object, but in Kotlin I couldnt really find something similar, or I could at least not get it to work. Is there an easy way to do this (setting UI from a class?) and is there something special I have to do inside the class?
I was quite dumbfounded by how difficult it is / how hard it is to find how to do it. I have things like and more
val textView = findViewById<TextView>(R.id.NewText)
textView.text = "Changed from class"
I also tried setting that inside
lifecycleScope.launch(Dispatchers.Main) {
}
and inside
runOnUiThread{}
both crashed!
I have tried doing somethings with binding and researched quite a bit and I just can't figure it out.
I saw some thing with something called kapt, but that is old I Believe?
Any help would be appreciated, thanks in advance.

Objective-C Passing Method Reference As Parameter

I've seen a lot of discussions NEAR this subject, but none that actually work in Xcode 5.x, especially using ARC. I have a simple problem:
I need to pass a method reference to a CreateButton method so that when the button is called it calls my custom function, and not some generic one.
I've tried using (SEL) type, but that doesn't work with ARC. I've tried using the &func method, but that claims I haven't declared the function yet.
So my need is:
Class A calls Class B and sends over the info to make a UIButton. Within that call, I want to send over the action:method in a reference. I'm sure this is done routinely, but I can't seem to find an iOS 7 / Xcode 5.x method of doing it. I've also reviewed the O'Reilly iOS 7 fundamentals and cookbook code and couldn't find this discussed anywhere.
Thanks for you help.
When I have to pass selectors around, I convert them to strings with NSStringFromSelector() and back to selectors with NSSelectorFromString().
Passing the strings around is a lot easier. You can store them in collections (arrays, dictionaries), serialize and unserialize them, and they will work naturally with ARC.
Example:
In your class A where you gather the information to create a button:
NSString *selectorString = NSStringFromSelector(#selector(yourActionMethodNameHere:));
// Gather more information needed by Class B here, then package
// it all up into a dictionary, for example
NSDictionary *buttonInfo = #{#"selectorString": selectorString, /* more stuff here */};
At this point, you can call your button-constructing method in Class B, passing along buttonInfo, which contains all the information that that helper method needs, including the selector. The method can convert the string back to a selector and use it like this:
SEL actionSelector = NSSelectorFromString(buttonInfo[#"selectorString"]);
// configure your button to use actionSelector here
You should be able to use SEL parameters? I know I have done before.
ARC might complain and give you a warning, but it won't fail to compile. It's simply a warning because it can't quite figure out what to do memory wise.
If you really can't get that to work though, another alternative would be to use a block, so you might call your method like
[objectA performMethodWithParam:paramA paramb:paramB completion:^{ ... do somethhing ... }];
Then in that method you can just call
completion();
Instead of actually calling a method.
Another alternative would be to use the delegate pattern. Create a #protocol defining a method such as classADidFinish then make class B implement that method. Then set the instance of classB as the delegate for your classA instance, and have it call that method when it's done.
Both of these approaches will stop ARC moaning at you.
But as I said, using SEL params should work fine. There is a way you can even get the compiler to stop showing you the warnings but it's a little ugly.

Easy way to tell if a method is overriding another method?

I'm just beginning with ObjC. I'm wondering how to find out when looking at code, written by me or from a template that comes when you use the wizard to create a new class, how you can tell if a method is overriding something.
In Java, you can mark a method with #Override, and then it's very easy to see if it's overriding something. That's not foolproof, because #Override is optional, but if I'm still unsure I can just type that in and see if it generates an error.
Is the only way to look up the source of the superclass, or in the case of a framework to read the documentation?
I don't know a way to see this immediately, but you could check if super responds
to the same selector. Example:
- (void)myMethod
{
// Temporarily add this line. If the compiler does NOT complain,
// "myMethod" overrides a method from some superclass.
[super myMethod];
// ...
}
You can use instancesRespondToSelector to see if your instance has an implementation of the method in its object hierarchy.
[MyClass instancesRespondToSelector:#selector(myMethod)];
or depending on what type of checking you need to do
[MyClassSuperClass instancesRespondToSelector:#selector(myMethod)];

Calling a class method from another class in Objective-C (Cocoa)

I'm new to programming in Cocoa, so I'm still struggling to grasp some basic concepts.
What I want to do (as an example) is write an application with multiple NSTextFields. However, these NSTextFields need to be linked to separate classes. Additionally, each separate class needs to be able to get and set data from each other.
I tried to add methods to tackle this problem, to no avail. Let's say this is a method in the textbox's original class, and I want to call it from another class.
-(void)settextfield:(NSString*)stringy;
{
[TextField setStringValue:stringy];
}
Here's the calling code (we're calling this from another class, TestClass)...
-(IBAction)test:sender;
{
[BundleBrowseTextBox settextfield: #"Testy"];
}
Nothing happens. There's probably some obvious way to do this, but I haven't been able to unearth this via Google searches.
My mistake was that I was calling the class method instead of the instance... you can call the instance via IBOutlets and defining those outlets properly in Interface Builder.
You need to make sure the pointers you are using are not nil.
One odd/convenient thing about objC is that you can pass messages to nil and it won't crash.
If I'm right in assuming you're trying to set the text in an instance of BundleBrowseTextBox, you should call the settextfield: message on the instance name, rather than on the class name (if BundleBrowseTextBox IS the instance -- rather than the class -- you should really avoid capitalized instance names for clarity). i.e.:
-(IBAction)test:(id)sender;
{
// Assuming bbtBox is defined as an instance of BundleBrowseTextBox
[bbtBox settextfield: #"Testy"];
}
I believe you forgot your parameter type in your original post
this...
-(IBAction)test:sender;
{
[BundleBrowseTextBox settextfield: #"Testy"];
}
should be
-(IBAction)test:(id)sender;
{
[BundleBrowseTextBox settextfield: #"Testy"];
}
That aside if you understand the difference between class and instance as you say you do.
Then it would be nice if you would show us the rest of your implementation and interface.
The problem is probably not in the code snippets you showed us.

Using a struct with OCMock or Hamcrest

I'm hitting a road block and I'm wondering if the brilliant collective minds here can help. In ObjC CocoaTouch I'm trying to mock an object that takes struct parameters and returns a struct. OCMock is coughing up a hair-ball so I tried wrapping with a Hamcrest matcher. No die. The function/method I'm testing looks something like this:
- (CLLocationCoordinate2D)pixelToLatLong:(CGPoint)aPoint;
I use code like this:
#define OCMOCK_STRUCT(atype, variable) [NSValue value:&variable withObjCType:#encode(atype)]
-(void) testMyWidget
{
CLLocationCoordinate2D ulLL = (CLLocationCoordinate2D){123,456};
CLLocationCoordinate2D lrLL = (CLLocationCoordinate2D){654,321};
[[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, ulLL)] pixelToLatLong:(CGPoint){0,0}];
[[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, lrLL)] pixelToLatLong:(CGPoint){320,460}];//lower right point
}
That kinda works. So in my object that I'm testing I make the necessary required edits to get a green bar... err.. green button in the build info window. When I'm certain that my test should pass I get assertion failed errors. The errors inform me that the method was invoked unexpectedly and lists the values for these structs as question marks. I tried wrapping the structs with Hamcrest matchers but I'm getting nowhere. I'm getting ready to break out my debugger which will no doubt show me what's wrong. Has anybody here had similar trouble with OCMock/Hamcrest and structs? If so, what's the best way to handle these types?
You're very close. Your #define should be:
#define OCMOCK_STRUCT(atype, variable) [NSValue valueWithBytes:&variable withObjCType:#encode(atype)]
The best answer is actually Cliff's himself: http://codeforfun.wordpress.com/2009/02/07/ocmock-return-a-struct/
He just didn't update this question, shame shame :)
I had problems with the macro answer; writing a helper function that returned the struct in the testing class and using:
[[[mockObject stub] andCall:#selector(selectorName) onObject:self] someMethod];
worked really well.
Sometimes a hand-coded mock is easier than trying to coerce a mock object framework outside of its normal use patterns.