Accessing NSMutableData from another class - objective-c

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.

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.

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

Related

How can I add a property "objective-c extension" to an anonymous class which implements a known protocol

I have an existing class for which I do not have the source, and I want to
add a property to the class. The private class implements a known protocol which is exposed, but the class type is not exposed.
Some callback happens and I receive the object named answer.
I want to extend the ComplexNumber type to have more properties,
e.g.
#interface NSObject()<ComplexNumber>
#property (assign) BOOL offline;
#end
#implementation SomeClass
didReceiveAnswer:id<ComplexNumber>answer forEquation:(NSString*)equation {
//
if (answer.offline) {
//
}
}
#end
This also fails:
Cast unknown type to be of type NSObject:
if (((NSObject*)answer).offline) {
//
}
There appear to be two issues here:
get access to the private class
add a property to it.
If you know the name of the private class you can simply use it be defining it again:
// SomeClass.h
#interface SomeClass : NSObject <ComplexNumber>
#end
This might seem odd, but this will be sufficient to pass the compilation stage of your build process and allow you to use the property in your code. The existing implementation of the private class will be sufficient to deal with the link stage.
As Daniele Pantaleone points out, the second part is very close to Objective-C: Property / instance variable in category. However I've added it for completness:
// ComplexNumber.h
#protocol ComplexNumber <NSObject>
#property (assign) BOOL offline;
#end
//ComplexNumber.m
#import ObjectiveC;
#implementation NSObject (ComplexNumber)
static void *ComplexNumberKey = &ComplexNumberKey;
-(void)setOffline:(BOOL)offline
{
objc_setAssociatedObject(self, &ComplexNumberKey, #(offline), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)offline
{
NSNumber *offline = objc_getAssociatedObject(self, &ComplexNumberKey);
return offline.boolValue;
}
#end

Objective C: Return an array from a class method

I have a NSMutablearray property in my class and i need to access it from another class what should i do ?? i tried to access it using a class method called breakfast , but it didnt see the property !!
This is my code
#interface FirstViewController : UIViewController {
NSMutableArray *food;
}
+ (NSArray*)breakfast;
#property (nonatomic , strong) NSMutableArray * food;
// in .m file
+ (NSMutableArray*)breakfast {
return self.food ; // here class method dont see the property called food
}
You can do something like this:
// FirstViewController.h
#interface FirstViewController : UIViewController {
}
+(NSMutableArray*) breakfast;
// FirstViewController.m
+(NSMutableArray*) breakfast
{
static NSMutableArray* _breakfast = nil;
if (_breakfast == nil)
{
// _breakfast alloc init
}
return _breakfast;
}
Biggest issue is that you cannot use "self.propertyname" inside static (+) methods. You need to have an instance of class to call self.
Other problem is that you have "Food" and "food" - different case in #property and getter you are trying to use - they are case-sensitive.
Also you have NSArray and NSMutableArray in .m and .h file.
On top of that - you don't really need this local variable declaration.
I think you described your problem in wrong way. If you want to pass data between your view controllers, check: Passing Data between View Controllers
If you want to have data available for applicataion in general - create singleton.

access private (Class extensions) members in subclass

I'm using a 3rd party class that contain the following extention:
#interface BaseClass ()
{
int privateMember;
}
#end
i've created my own subclass:
#interface SubClass : BaseClass {
}
#end
is there a way to access privateMember in SubClass?
EDIT:
actual code
GPUImageMovie.m: (base class)
#interface GPUImageMovie ()
{
BOOL audioEncodingIsFinished, videoEncodingIsFinished;
GPUImageMovieWriter *synchronizedMovieWriter;
CVOpenGLESTextureCacheRef coreVideoTextureCache;
AVAssetReader *reader;
}
MultiTrackGPUImageMovie.h (subclass)
#interface MultiTrackGPUImageMovie : GPUImageMovie {
}
...
#end
MultiTrackGPUImageMovie.m (subclass)
- (void)processMovieFrame:(CMSampleBufferRef)movieSampleBuffer forTarget:(int)targetToSendIdx {
...
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, coreVideoTextureCache, movieFrame, NULL, GL_TEXTURE_2D, GL_RGBA, bufferWidth, bufferHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0, &texture);
...
}
give error
Use of undeclared identifier 'coreVideoTextureCache'
It depends on how the 'private' member has been declared. If there wasn't the #private keyword before it, i. e. it was really
#interface BaseClass ()
{
int privateMember;
}
#end
and not
#interface BaseClass ()
{
#private
int privateMember;
}
#end
then you can easily reference this instance variable simply by using its name - the default access scope for instance variables is protected, i. e. not accessible outside the class, but accessible from subclasses.
However, if it was declared as private, you'll have to fall back using the runtime functions; in your subclass, declare and implement this method:
- (void *)pointerForIvarWithName:(NSString *)name
{
Ivar ivar = class_getInstanceVariable([self class], [name UTF8String]);
return ((char *)self) + ivar_getOffset(ivar);
}
Then use it like this:
int ivarPtr;
ivarPtr = *(int *)[self pointerForIvarWithName:#"privateMember"];
Edit: so it seems the member you're trying to access is in a class extension and not public in the header file. In this case, you can go for the 2nd solution only (although it's not advised to do so).
For me it is impossible to use the default #protected ivar from the class extension in the subclass like said the the accepted answer, when the class extension is in the .m file.
So here is how I could solve this issue without using the runtime API (which should not be used in such a manner).
They are implemented, also for the subclass `SubClass. You can not access them, because the declaration ist not visible to the subclass.
The subclass does not have access to the class extension #interface BaseClass () of the BaseClass.
You have 2 options:
Copy
#interface BaseClass () {
int privateMember;
}
#end
to every subclass. Yes it is the class extension of the BaseClass inside the .m file of the SubClass.
OR
Create a .h file without a .m, only with the class extension code (like above) inside. Import that to the BaseClass and the SubClass.

Accessing public fields in Objective C

I've tried the following sample code:
#import "Foundation/Foundation.h"
#interface example
{
#public NSString* name;
}
#end
#implementation example #end
int main()
{
example* me;
me->name = #"World";
}
And it appears my code hates me at this point. I do understand how much of a bad idea it is to make a field public, but I'm not sure why I'm getting an error at that last line in main().
There is a lot wrong here
You class should subclass NSObject so it should be declared as
#interface example : NSObject
{
#public NSString* name;
}
#end
Next you actually need an instance of the class to get at it's values e.g.
example *me = [[example alloc] init];
NSLog(#"%#", me->name);
Next classes are named starting with an uppercase letter and normally have a prefix e.g. I would use
PSExample
Next please don't access instance variables like this, you should make your objects state available through accessors rather than give direct access.
You're not allocating or initializing your me variable. You probably want to inherit from NSObject and then use this:
example *me = [[example alloc] init]
At the very least you need to alloc it.
You need to initialize your variable before you can access it. Also you should derive your class from NSObject.
#import "Foundation/Foundation.h"
#interface example : NSObject
{
#public NSString* name;
}
#end
#implementation example #end
int main()
{
example* me = [[example alloc] init];
me->name = #"World";
}

subclassing issue with static constant variable in iOS

I have Class A and Class B.
Class B subclasses Class A
Class A has a static constant NSString variable
In a method of Class B I need to use the static constant NSString variable of Class A. What are my options ?
I tried declaring the same thing again, but caused problem (internal inconsistency), tried without using (says, variable undeclared).
Any idea how I can solve this problem ?
Thanks.
Make a class method returning that constant, like this:
+(NSString*) constString {
return myConstString;
}
You can declare static constant strings in the .h file. That way they are public and can be used by other classes that import the header file, including your subclass.
Alternatively, you can declare a reference to the string in your Class B using extern:
extern NSString *const MyString;
That basically tells the compiler that the value of that string is defined elsewhere in the code and it doesn't have to worry about it.
By "Class A has a static constant NSString variable", I assume you're referring to having something like the following defined in your Class A .m file:
static NSString * const MyString = #"MyString";
#implementation ClassA
#end
To allow Class A and its subclasses to see the value, you can do something like this:
MDClassAPrivate.h:
#import <Foundation/Foundation.h>
static NSString * const MDBlahBlahKey = #"MDBlahBlah";
MDClassA.h:
#import <Foundation/Foundation.h>
#interface MDClassA : NSObject {
}
#end
MDClassA.m:
#import "MDClassA.h"
#import "MDClassAPrivate.h"
#implementation MDClassA
#end
MDClassB.h:
#import "MDClassA.h"
#interface MDClassB : MDClassA {
}
#end
MDClassB.m:
#import "MDClassB.h"
#import "MDClassAPrivate.h"
#implementation MDClassB
#end
By moving the static const variables to a separate file, you can allow any class to import them in the implementation file.
My personal solution to this problem would be a pseudo-protected method that returns the constant string.
// ClassA.m
#interface ClassA ()
- (NSString *)constantString;
#end
#implementation ClassA
- (NSString *)constantString {
return #"MyConstantString";
}
#end
Then following on into your subclass:
// ClassB.m
#interface ClassA ()
// This is a method redeclaration to avoid build warnings
- (NSString *)constantString;
#end
#implementation ClassB
- (void)someMethod {
NSString *theConstantString = [self constantString];
// do stuff...
}
#end
The assumption here is that your constant string really is constant, if you want it to be dynamic you would need to modify this solution slightly, but it can still handle it.
You can use 'userDefaults' to set a value that is accessible in the whole application.
[[NSUserDefaults standardUserDefaults] setObject:object forKey:#"objectKey"];