creating instances in objective c - objective-c

Here is my code:
//ECHOAppDelegate.m
#implementation ECHOAppDelegate
...
#end
//PtyView.m
#interface PtyView (PtyPrivate)
-(void)startTask;
-(void) didRead: (NSNotification *)fileNoty;
#end
#implementation PtyView
...
-(void)startTask {
//starts task
}
#end
Now, how do I trigger "startTask" from ECHOAppDelegate.m? I need to create an instance? I'm a total beginner :D
Any example code would be awesome!
Thanks,
Elijah

-(void)startTask; appears to be private implementation and in theory should not be called from external classes.
To answer your question, you can call it something like this:
PtyView *v = [[PtyView alloc] init];
[v startTask];
[v release];
Though you will get a warning saying, PtyView might not respond to startTask. Since it is not in public interface of class.
Update: Above code assumes that when startTask returns, you are done with this object. But something tells me that you might be using async callbacks. If that is the case then startTask might return immediately and you won't release it then and there. Normally in this case, you will be notified by PtyView about the completion of task. So you release it when the task is complete.
Update2:
Making a method public is easy. You just declare it in the public interface (the header file of class):
//in PtyView.h
#interface PtyView
-(void)startTask;
#end
//in PtyView.m
#implementation PtyView
...
-(void)startTask {
//starts task
}
#end
Notice that there is no category defined in the interface declaration.

self represent the current object.
You just need to call the method like that.
[self startTask];

How about subclassing ECHOAppDelegate? (Then make sure PtyView inherits from NSObject?)
// cf. http://amath.colorado.edu/pub/mac/programs/PseudoTTY.zip
#interface ECHOAppDelegate : PtyView
...
#end
#implementation ECHOAppDelegate
- (id) init
{
self = [super init];
if (!self) return nil;
...
return self;
}
...
[self startTask];
...
#end

Related

Delegating the even callback in objective-C

I am trying to wrap my head around how does the an event callback delegation works. So far I have written following code which btw works just fine:
Bridge.h
#protocol BridgeDelegate <NSObject>
- (void) bridgeLock;
#end
#interface Bridge : NSObject
+(instancetype) sharedInstance;
#property (weak, nonatomic) id<BridgeDelegate> bridgeDelegate;
- (void) wipe;
#end
Bridge.m
#implementation Bridge
+(instancetype) sharedInstance {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] initUniqueInstance];
});
return shared;
}
-(instancetype) initUniqueInstance {
return [super init];
}
- (void) wipe
{
NSLog(#"lock in bridge called");
if(self.bridgeDelegate)
{
[self.bridgeDelegate bridgeLock];
}
}
#end
Plugin.h
#interface Plugin : NSObject<BridgeDelegate>
#property (strong, nonatomic) Bridge *bridge;
- (void) pluginInitialize;
#end
Plugin.m
#implementation Plugin
- (void) pluginInitialize
{
self.bridge = [Bridge sharedInstance];
self.bridge.bridgeDelegate = self;
}
- (void)bridgeLock
{
NSLog(#"lock in plugin called");
}
#end
When I call the following code in applicationDidBecomeActive
Bridge* bridge = [Bridge sharedInstance];
Plugin* plugin = [[Plugin alloc] init];
[plugin pluginInitialize];
[bridge wipe];
I get the following expected output:
lock in bridge called
lock in plugin called
Now my questions:
How exactly is the delegate work? In the sense, Plugin is only implementing the function bridgewipe(), right? Why and how bridgeLock is being called at first place?
Does this have anything to the fact that Bridge is a singleton. Had I made Bridge a non singleton class, will the end result be same.
1.How exactly is the delegate work? In the sense, Plugin is only implementing the function bridgewipe(), right? Why and how bridgeLock is being called at first place?
In the above pasted code "Plugin.m" is implementing - (void)bridgeLock
Does this have anything to the fact that Bridge is a singleton. Had I made Bridge a non singleton class, will the end result be same.
No
Bridge* bridge = [Bridge sharedInstance];
this bridge we call it B1;
B1 is a instance make by the method "sharedInstance";
then you call the following code :
Plugin* plugin = [[Plugin alloc] init];
[plugin pluginInitialize];
pluginInitialize method your code is
{
self.bridge = [Bridge sharedInstance];
self.bridge.bridgeDelegate = self;
}
when code executed, self.bridge is also a instance make by the method "sharedInstance"; it's equal to B1 with address and also make B1's delegate == self;
so when you call [bridge wipe];
It will nslog #"lock in bridge called";
Because self.bridgeDelegate is not nil, so delegate will call the bridgeLock method;
Then nslog #"lock in plugin called".
About your second question, when you make Bridge a non singleton class, I think the result will be different.
#hariszaman, explanation is right but I would like to expand more on this so it can help someone in the future. Basically this is what happening.
I am creating an instance of the Bridge class. This instance in the memory has a reference variable of type BridgeDelegate.
As I instancetiate Plugin, BridgeDelegate variable starts pointing to the the Plugin class instance.
Now when lock is called, it calls the bridgelock method of the class that is pointed by BridgeDelegate pointer which in this case is Plugin.
It doesn't matter if the Bridge class is not singleton because following line in pluginInitialize:
{
self.bridge = [Bridge sharedInstance];
self.bridge.bridgeDelegate = self;
}
will be changed to :
{
self.bridge = [[Bridge alloc] init];
self.bridge.bridgeDelegate = self;
}
and steps 1,2 and 3 will be repeated the same way.

Call method from parent class

I have initialised a custom class from within another class, I then want to run a function from the custom class and when it's done call a method from the class which initialised it.
// classA.m
-(void)methodA {
// do some complicated stuff
if (result) {
[classB methodB];
}
}
// classB.m
-(void)methodA {
classAInstance = [[classA alloc] init];
[classAInstance methodA];
}
-(void)methodB {
// got result, do more stuff
}
[classB methodB] doesn't work, but I have no clue how this would be achieved so any help would be greatly appreciated, thanks.
I'm new to objective-c so please bear with me.
I'll keep my clothes on for this one--if you don't mind.
One way to achieve what you want is through 'composition', which means write A so that it has a member variable that is an instance of B. Then A can use that instance of B to call methods in B:
A.h:
#import <Cocoa/Cocoa.h>
#import "B.h"
#interface A : NSObject {
B* my_b;
}
- (id)init:(B*)b;
- (void)methodA;
#end
.
A.m:
#import "A.h"
#implementation A
- (id)init:(B*)b
{
if (![super init])
{
return nil;
}
my_b = b;
return self;
}
- (void)methodA
{
[my_b methodB];
}
#end
.
B.h:
#import <Cocoa/Cocoa.h>
#interface B : NSObject {
}
- (void)do_stuff;
- (void)methodB;
#end
.
B.m:
#import "B.h"
#import "A.h"
#implementation B
- (void)do_stuff
{
A* a = [[A alloc] init:self];
[a methodA];
}
- (void)methodB
{
NSLog(#"hello");
}
#end
===
Because you wrote:
[classB methodB];
...maybe you want to call a class method in B.
A.h:
#import <Cocoa/Cocoa.h>
#import "B.h"
#interface A : NSObject {
}
- (void)methodA;
#end
A.m:
#import "A.h"
#import "B.h"
#implementation A
- (void)methodA
{
[B classMethodB];
}
#end
B.h:
#import <Cocoa/Cocoa.h>
#interface B : NSObject {
}
+ (void)classMethodB;
- (void)do_stuff;
#end
B.m:
#import "B.h"
#import "A.h"
#implementation B
- (void)do_stuff
{
A* a = [[A alloc] init];
[a methodA];
}
+ (void)classMethodB //Note the '+'
{
NSLog(#"hello");
}
#end
I think there is something very crucial that is being overlooked here by the other posters: retain cycles. Any child method that is trying to reference its parent object needs to do so with a weak reference or an __unsafe_unretained modifier. If you don't you run the risk of having the parent object caught in a retain cycle. If I understand the question, you simply want to call a method in a class 'A' object when a certain method finishes in a class 'B' object? I usually do this one of two ways: delegates and protocols (the more difficult concept) or NSNotificationCenter (the less difficult concept). In your case, since you are just trying to 'notify' one method when another method in a different class completes, the notification center seems like it would be far easier to use. There is a pretty good tutorial here: http://blog.isotoma.com/2009/11/on-objective-c-delegates-and-nsnotification-objects/ but here is the basic premise:
In the method doing the work (in Class B) at the end of the method you insert something like this:
NSNotification* notification = [NSNotification notificationWithName:#"MyNotification" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
Then, in the class A init method you would register to receive that notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MethodToCallAfterNotification:) name:#"MyNotification" object:nil];
Whenever the class B method finishes, it will broadcast a 'MyNotification' notification. Your class A object is listening for those notifications, so it will automatically call whichever selector you designate whenever that notification is broadcast in your app.
Just be sure to create a dealloc method in your class A implementation file and unregister the observer like this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"MyNotification" object:nil];
If you are interested in learning how to do this with a weak referenced delegate method call, I posted an answer about that here: https://stackoverflow.com/a/10273551/1318525
#classA.m
-(void)methodA:(classB *)classB {
#classB.m
[classAInstance methodA:self];
Before calling the method you have to initialize a object of the class to call a explicit method ... like as
// classA.m
- (void)somethingA{ }
Your code will work if the method is implicit method of the class, where you don't need to create or initialize classes object, but remember that implicit method can only called by the class.. like as
// classB.m
+ (void)somthingB {}

Objective-C methods not running

I'm messing around with using objects to launch background threads, however when I call an objects method to call the method that will spawn a background thread, nothing happens. I'm a bit puzzled as to why, and it looks like the -init function isn't even being called. Anyways, here's what I have:
ViewController.h
#import <UIKit/UIKit.h>
#import "Threader.h"
#interface ViewController : UIViewController
#property(nonatomic, strong) Thread* threadedObject;
- (IBAction)StartBackgroundThreadButtonClicked:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "Threader.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_threadedObject = [[Threader alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)StartBackgroundThreadButtonClicked:(id)sender {
NSLog(#"Clicked.");
[_threadedObject RunInBackground];
}
#end
Threader.h
#import <Foundation/Foundation.h>
#interface Threader : NSObject
#property(nonatomic) bool IsFinishedRunning;
#property(nonatomic) bool IsThreading;
//Constructor and Destructor
-(id)init;
-(void)dealloc;
-(void)RunInBackground;
-(void)WaitForTenSeconds;
#end
Threader.m
#import "Threader.h"
#implementation Threader
//constructor
-(id)init{
[super init];
if(self != nil)
{
_IsFinishedRunning = NO;
_IsThreading = NO;
}
return self;
}
//destructor
-(void)dealloc{
[super dealloc];
}
//Runs a thread in the background
-(void)RunInBackground{
NSLog(#"Initiating thread...");
[self performSelectorInBackground:#selector(WaitForTenSeconds) withObject:nil];
}
//Waits for 10 seconds, then sets IsFinishedRunning to YES
-(void)WaitForTenSeconds{
NSLog(#"Starting to run in the background.");
_IsThreading = YES;
sleep(10);
_IsFinishedRunning = YES;
NSLog(#"Finished running in the background.");
}
#end
When I run the program, this is my output(I clicked the button a few times)
2013-05-17 15:30:57.267 ThreadedObjects Clicked.
2013-05-17 15:30:59.003 ThreadedObjects Clicked.
2013-05-17 15:30:59.259 ThreadedObjects Clicked.
2013-05-17 15:30:59.443 ThreadedObjects Clicked.
2013-05-17 15:30:59.675 ThreadedObjects Clicked.
I should be getting messages telling me that the Threader object was created, and that it is preparing to launch a background thread, that the thread has been spawned and then after 10 seconds, that the thread is done running.
So, where's my glaring obvious error?
init isn't a constructor, it's for setup after construction. You need the class object to create an instance before you can send init, and, most importantly, you need to assign the results to your variable.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
threadedObject = [[Threader alloc] init];
}
You can't send alloc to an object that's not a class; instances don't respond to it. The only reason that this isn't crashing is that globals are initialized to 0/NULL/nil, and [nil someMessage] does nothing.
Not assigning the results to your variable is the same as:
int x = 0;
x + 10;
There's no change to x's value.
Additionally, you don't seem to have an ivar there, just a global variable. Ivars need to go into a curly-brace block at the head of the #implementation:
#implementation Threader
{
Threader * threadedObject;
}
// etc...
You never alloc the object.............
Also, this is curious:
#import <UIKit/UIKit.h>
#import "Threader.h"
#interface ViewController : UIViewController
- (IBAction)StartBackgroundThreadButtonClicked:(id)sender;
#end
Threader* threadedObject;
Where exactly did you declare the threadedObject? Like above? Use an iVar
or, better, a property for it!
A couple of reactions:
Show us where your definition and alloc/init of threadedObject.
I'm not sure what business problem you're trying to solve, but this smells like the precursor of some custom NSOperation solution. Operation queues are ideally suited for these sorts of implementations.
I'd be inclined to subclass NSOperation when trying to do something like this. See the custom NSOperation object in the Concurrency Programming Guide.
I'd suggest using camelCase for your method and variable names.
If you say with this, I'd steer you away from the "thread" name, as it might imply that you're doing something with NSThread, which you're not.

Passing data between objects/threads in Cocoa

I create a new thread from a controller like this:
[NSThread detachNewThreadSelector:#selector(makeMovie) toTarget:movieMaker withObject:nil];
What is the best way to call methods on the controller from the new thread/movieMaker object?
In this case, you probably want to use the delegate pattern. In your movie-maker class’s .h file:
#protocol MovieMakerDelegate;
#interface MovieMaker : NSObject
{
id<MovieMakerDelegate> delegate;
...
}
#property (nonatomic, assign) id<MovieMakerDelegate> delegate;
...
#end
#protocol MovieMakerDelegate <NSObject>
- (void)movieMaker:(MovieMaker *)maker didSomething:(id)result;
#end
...and in its .m:
#implementation MovieMaker
#synthesize delegate;
...
- (void)makeMovie
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
...
[self performSelectorOnMainThread:#selector(notifyDelegateOfDoingSomethingWithObject:) withObject:someObject waitUntilDone:YES];
[pool release];
}
- (void)notifyDelegateOfDoingSomethingWithObject:(id)someObject
{
[self.delegate movieMaker:self didSomething:someObject];
}
...
#end
Then in your controller’s header, declare that it supports the MovieMakerDelegate protocol, like this:
#interface MyController : Whatever <MovieMakerDelegate>
and in your .m, before calling your -detachNewThreadSelector:etc:, call movieMaker.delegate = self;. Implement the -movieMaker:didSomething: method on your controller class and you’re good to go.
You may pass the controller instance to your makeMovie method, with the withObject parameter.
[NSThread detachNewThreadSelector:#selector(makeMovie) toTarget:movieMaker withObject:self];
You can also use a singleton-like pattern, if applicable, to retrieve your controller instance from the thread.
Use performSelectorOnMainThread:withObject:waitUntilDone: (or similar performSelector:... methods), it is the easiest way.

Objective-C inheritance; calling overridden method from superclass?

I have an Objective-C class that has a method that is meant to be overridden, which is uses in a different method. Something like this:
#interface BaseClass
- (id)overrideMe;
- (void)doAwesomeThings;
#end
#implementation BaseClass
- (id)overrideMe {
[self doesNotRecognizeSelector:_cmd];
return nil;
}
- (void)doAwesomeThings {
id stuff = [self overrideMe];
/* do stuff */
}
#end
#interface SubClass : BaseClass
#end
#implementation SubClass
- (id)overrideMe {
/* Actually do things */
return <something>;
}
#end
However, when I create a SubClass and try to use it, it still calls overrideMe on the BaseClass and crashes due to doesNotRecognizeSelector:. (I'm not doing a [super overrideMe] or anything stupid like that).
Is there a way to get BaseClass to call the overridden overrideMe?
What you are describing here should work so your problem is likely elsewhere but we don't have enough information to help diagnose it.
From your description, I'd say either the instance you're messaging is not the class you think it is or you made some typo in your code when declaring the method names.
Run your application under gdb, add a symbolic breakpoint on objc_exception_throw, reproduce your problem. Once your process has stopped on the "doesNotRecognizeSelector" exception, print object description and it's class.
Or log it before calling -overrideMe:
NSLog(#"object: %# class: %#", obj, [obj class])
Write a category for BaseClass to override the method.
#interface BaseClass (MyCategory)
- (id) overrideMe;
#end
#implementation BaseClass (MyCategory)
- (id) overrideMe
{
/* Actually do things */
return <something>;
}
#end
Now all instances of BaseClass will respond to selector overrideMe with the new implementation.