0

Ok I have declared the NSMutableData in the .h of class 1 as followed

NSMutableData *dataResponse;

@property (strong, nonatomic) NSMutableData *dataResponse;

in the .m of class 1 I have @synthezie dataResponse, and then I am giving it some data in a function.

I want to access dataResponse in class 2 with that data that I have assigned to it in the function.

How can I get the data from dataResponse in class 2? Any help would be great.

Popeye
  • 11,839
  • 9
  • 58
  • 91

2 Answers2

2

You can use helper class for accessing array in different class. Create an NSObject file in the project. I've named it Passing Class

In your PassingClass.h

#import <Foundation/Foundation.h>

@interface PassinClass : NSObject
{
  NSMutableData *dataResponsetoPass;
}
+(PassinClass*)sharedString;


-(void)setdataResponsetoPass:(NSMutableData*)data;
-(NSMutableData*)getDataResponse;

In your PassinClass.m

#import "PassinClass.h"

@implementation PassinClass
@synthesize dataResponsetoPass;
static PassinClass*sharedString;

+(PassinClass*)sharedString
{
 if(!sharedString)
 {
    sharedString=[[PassinClass alloc]init];
  }

  return sharedString;
}


-(void)setdataResponsetoPass:(NSMutableData*)data
{
  dataResponsetoPass=data;
}
-(NSMutableData*)getDataResponse;
{
   return dataResponsetoPass;
}

In your class1.h create instance of this helper class.

#import "PassinClass.h"
{
  PassinClass*pClass;
}

In your class1.m, set the data using

pClass=[PassinClass sharedString];
[pClass setdataResponsetoPass:Your Data];

In your class2.m get the data using

pClass=[PassinClass sharedString];
[pClass getDataResponse];

NSLog the [pClass getDataResponse ] to check, if everything went well you should be able to pass the response data from class 1 to class 2.

iNoob
  • 3,364
  • 2
  • 26
  • 33
  • That's basically a "singleton" -- search on the term and you'll find lots of "patterns" for this. The other common way to handle global data is with a property in your AppDelegate object. Of course, it's better, where possible, to pass your object address as a parameter. – Hot Licks Dec 13 '11 at 12:27
  • @HotLicks yes you are right this is a singleton, i've done it through appdelegate object too, but after some research that was mentioned as "unclean" method. Won't this piece of code do his job ? Or are there any better methods? – iNoob Dec 13 '11 at 12:32
  • Nothing "unclean" about using AppDelegate. There are some snobs that don't like to use it, but mostly because they don't have the discipline to maintain it properly. In terms of singletons, since I hardly ever use them I haven't settled on a preferred "pattern". – Hot Licks Dec 13 '11 at 13:16
  • Ok thanks, i've read on multiple answers that AppDelegate is only for applications core functionalities and that it shouldn't be bothered with passing of strings,data etc. from one class to another. – iNoob Dec 13 '11 at 13:22
-1

Create an instance of the class and use the -mutableByes method. If you still need more information, check out the class reference for NSMutableData, right here

Monkeyanator
  • 1,346
  • 2
  • 14
  • 29
  • Creating a new instance of the class won't return the value already set, but will instead nil. – Hot Licks Dec 13 '11 at 12:29
  • I don't believe that my answer deserved a down vote... if you knew how to use the answer to your advantage, you could have solved you're problem :P – Monkeyanator Dec 26 '11 at 00:25