I need to create a class that gets a delegate does some calculation and then calls the delegate.
What is the best practice to do such a thing? (I am trying to create a web service class which reads data, and as soon as it's done it sends back the data to the delegate and it destroys itself)?
-(void)viewDidLoad { MyClass *class = [[MyClass alloc] initWithDelegate:self]; }
-(void) MyClassRespond :(NSData*)data { //use data and populate on screen }
The class can't destroy itself, and you can't init a class object without allocating it (one of the issues being where exactly do the fields get stored without some stuff.
I am not sure why it needs to destroy itself, but the usual method to do this would be to have a variable of type MyClass* _class in the class with the viewDidLoad method. You would then do the equivalent of
-(void)viewDidLoad { _class = [[MyClass alloc] initWithDelegate:self]; }
-(void) MyClassRespond :(NSData*)data {
[_class release];
//use data and populate on screen }
You could of course also do something where you pass MyClass back to the delegate method, so something like
-(void)viewDidLoad {MyClass *class = [[MyClass alloc] initWithDelegate:self]; }
-(void) MyClassRespond :(NSData*)data fromClass: (MyClass*)class {
[class release];
//use data and populate on screen }
In this case your delegate would call MyClassResponds as
[delegate MyClassRespond:data fromClass:self];
It might also make sense to try and make MyClass reusable so you don't need to dispose it quite as quickly, but I understand there are cases where this doesn't work.