Messaging between two classes - objective-c

I have a basic question about fetching values through different classes.
I have a classA which fills an array (If i print it out it is not empty).
LATER in class B i want to load this Array: I call a function from class A which returns the Array of class A. But in class B if i call my new array then is it null.
I am a bit confused, because i think i retain every value of the array, but its still null. I tried also a lot of different possibilities. I think its a basic OOP syntax fault i produce?!
//CLASS_A.h
#interface classA {
NSMutableArray* buoyArray;
}
#property (nonatomic, retain) NSMutableArray * buoyArray;
-(NSMutableArray*)getArray:(NSMutableArray*)_array;
//CLASS_A.m
...
-(NSMutableArray*)getArray:(NSMutableArray*)_array {
_array=buoyArray;
return _array;
}
//CLASS_B.h
#import "CLASS_A.h"
#class classA;
#interface classB ...
classA *mapSource;
NSMutableArray * buoyArray;
}
#property(nonatomic,retain) classA *mapSource;
//CLASS_B.m
buoyArray=[mapSource getArray:buoyArray];
NSLog(#"%#",buoyArray);

Actually you you are making new object of class A by calling alloc so by init it reintialize all properties values for that instance.
What you need,if you are pushing class B over class A then, fetch existing class A object from stack, by using this line.
mapSource = (ClassA *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2];
then call this
buoyArray=[mapSource getArray:buoyArray];
NSLog(#"%#",buoyArray);

Related

Objective C: array of class object as property

Is it possible in an Objective-C class to store an array of instances of another class as a property?
Simple example: I have a class called "Classroom" and a class called "Students".
#interface Student
#property int m_id;
#end
...
#interface Classroom
#property Student *m_students[20]; // this causes a compilation error: property cannot have an array or function type 'Student *[20]'
#end
How can I do the equivalent of this?
Use a NSArray or NSMutableArray instead:
#interface Classroom
#property NSMutableArray *m_students; // this causes a compilation error.
#end
Then in your implementation:
Student *student = [[Student alloc] init];
[self.m_students addObject:student];
NSArray (and it's subclass NSMutableArray) can contain any Objective-C object. And you can even mix them in the same array.

Variable losing value in delegation pattern

I'm trying to learn about delegation in Objective-C, and am having a minor issue with a variable losing it's data in the transfer process. I have Class1 that contains an NSMutableArray. The array gets populated, then I would like to transfer the array's values to Class2, and display it. Here is the relevant code in Class1:
//Class1.h
#class Class1;
// define the protocol for the delegate
#protocol Class1Delegate
#required
-(void)sayHello:(Class1 *)customClass withAntArray:(NSMutableArray *)antArray;
#end
#interface Class1 : MySuperClassName
#property (nonatomic, assign) id delegate;
-(void)helloDelegate;
#end
//Class1.m:
#interface Class1 ()
#property (nonatomic, strong) NSMutableArray *antOccurenceTimes;
#end
#implementation Class1
#synthesize antOccurenceTimes;
-(void)helloDelegate
{
// send the message to the delegate
[_delegate sayHello:self withAntArray:self.antOccurenceTimes];
}
Now, this is what I have in Class2:
#import "Class1.h"
#interface Class2 : UIView <Class1Delegate>
#end
// Class2.m:
- (void)appropriateTimeToCallMethod {
Class1 *initAntMarks = [[Class1 alloc] init];
initAntMarks.delegate = self;
[initAntMarks helloDelegate];
}
-(void)sayHello:(Class1 *)customClass withAntArray:(NSMutableArray *)antArray {
NSLog(#"Hello! %#", antArray.description);
}
The antArray.description reads as "NULL". Now, I figured that obviously it will be null, because I just created an entirely new instance of the class right before calling upon the needed method. I feel like I may have something mixed up, and being so new to delegation, I'm not sure exactly what. Does anyone know what I need to tweak to utilize Delegation?
I forgot to add that I did initialize it in Class1, and it gets populated just fine. It's only in class2 that this is occurring.
I initalize antOccuranceTimes in a separate method in ClassA in the snippet below, and the NSLog fires twice...
NSLog(#"Array initalized in class A");
antOccurenceTimes = [NSMutableArray new];
Change this line:
#property (nonatomic, assign) id delegate;
to:
#property (nonatomic, weak) id <Class1Delegate> delegate;
assign should only be used for C primitives, not Objective-c object references. You should also be checking if your object actually conforms to the delegate before messaging the delegate.
Edit:
I think you may be confused about the purpose of delegation.
Class1 *initAntMarks = [[Class1 alloc] init];
initAntMarks.delegate = self;
[initAntMarks helloDelegate];
Why are you calling a method on an object which in turn calls a delegate method when you could simply create a method that returns the NSMutableArray? The way you have your code currently set up requires that before the call to -helloDelegate you have to have filled the array with the appropriate objects. The purpose of delegation in MVC is to inform an object about an event that took place inside of another object. You are "delegating" the task off to another object, or you could say, that another object if responsible for the fulfillment of the task. Read the Apple Docs on Delegation. Delegation in your code is not the correct pattern to implement, as I stated you can simply return that array with a method call.
Edit 2:
There are two ways you can achieve this, through property methods or through an explicit method that returns your array. If you choose to use property methods, the property declaration must be in the public interface i.e. the .h file so that your class can all the accessors when the object is being implemented.
//Inside the .h
#property (nonatomic, strong) NSMutableArray *antOccurenceTimes;
This will automatically provide you with two accessor methods for the antOccurenceTimes property. These are the getter -antOccurenceTimes and setter -setAntOccurenceTimes: methods. Now after you initialize the class and fill your array you can call -antOccurenceTimes to return the array.
You can also create an explicit method that return the array:
- (NSMutableArray *)hello{
//Do something here
return _antOccurenceTimes;
}
You have not yet initialized the antOccurenceTimes. Of cause it is nil. There are many options depending on what you need. You can, for example, initialize it in a init function:
- (instancetype)init {
self = [super init];
if( self ) {
antOccurenceTimes = [NSMutableArray array];
[antOccurenceTimes addObject:#"Hello World"];
}
}
Or maybe initialize it before you call the delegate the function.
-(void)helloDelegate
{
// send the message to the delegate
self.antOccurenceTimes = [NSMutableArray array];
[self.antOccurenceTimes addObject:#"Hello World"];
[_delegate sayHello:self withAntArray:self.antOccurenceTimes];
}
I think you get my point.

What is the difference between declaring a member in the extended interface versus in the implementation?

I am seeing two very different behaviors for something that I thought were the exact same.
Defining my private member in the class extension like this:
#interface ClassA ()
#property ClassB* b;
#end
#implementation ClassA
-(ClassA*)initWithClassB:(ClassB*)newB
{
self.b = newB;
return self;
}
-(ClassB*)getB
{
return self.b;
}
Or defining my private member in the class implementation like this:
#interface ClassA ()
#end
#implementation ClassA
ClassB* b;
-(ClassA*)initWithClassB:(ClassB*)newB
{
b = newB;
return self;
}
-(ClassB*)getB
{
return b;
}
The way I am using this code is to create a ClassB object, initialize a ClassA object with that ClassB object, and then add the ClassA object to a mutable array
-(void)init
{
self.classAList = [[NSMutableArray alloc] init];
[self.classAList addObject:[[ClassA alloc] initWithClassB:[self createClassB1]]];
[self.classAList addObject:[[ClassA alloc] initWithClassB:[self createClassB2]]];
[self.classAList addObject:[[ClassA alloc] initWithClassB:[self createClassB3]]];
}
-(ClassB)createClassB1
{
ClassB* classB = new ClassB();
//do some init
return classB;
}
// Same thing fore createClassB2 and createClassB3 except with different data
When I use the first approach, and define my member in the interface extension, I see that each element in my mutable array is indeed what I would expect.
However, using the second approach, I see that my ClassB* b pointer in the ClassA object always ends up pointing to the most recently created ClassB object. That is, once the -(void)init method finishes, the ClassB pointers in each of the ClassA objects points to the ClassB object I created in createClassB3
What is happening here?
I should also mention that the ClassB object is a C++ object and this is a an objective-c++ class.
In your second snippet, b is just a global variable at file scope. The fact that it's inside of the #implementation ... #end is irrelevant. It is not an instance variable nor a property.
With the second approach you're creating a global variable, meaning it's not related to any instance of ClassA, so you will always have one and the same instance of *b pointing to the same object in memory. So anytime you change the value of the *b you're changing the object in memory at which the b variable is pointing, but never creating a new one; to understand it better you're basically initialising every ClassA object with the same ClassB variable (which is *b), so if you change the value at the portion of memory to which *b is pointing you're changing it for all the instances of ClassA created.
Hope it's clear enough.

Class Method Exposure and Property Definition

I have an academic question about Class Method exposure. There is something that I obviously don't understand about this and would like some clarification from those in the know.
Background:
I have a simple example of two classes named ViewController and ClassB. Class B contains an array with a method named returnArray. The ViewController accesses the array's data. I have exposed the returnArray method in the ClassB.h file.
Question:
Why is it that I can access the array's data in ViewController without having to define a property? I thought that the property would create a getter to allow access to the array. My example (only exposing the method) allows me to access the data without the creation of the #property.
Class Method:
ClassB.h
#interface ClassB : UIViewController
+(NSArray *) returnArray;
//#property (nonatomic, strong) NSArray *returnArray;
ClassB.m
#implementation ClassB
+(NSArray *) returnArray
{
NSArray *locationArray = #[#"Place1", #"Place2"];
return locationArray;
}
ViewController.m
- (void)viewDidLoad
{
NSArray *location = [ClassB returnArray];
NSLog (#"The count of the location is %d", [location count]);
NSLog (#"The second item in testArray is %#", location[1]);
}
Instance method: After reviewing answers
ClassB.h
*/
{
#private
NSArray *returnArray;
}
- (void)setReturnArray:(NSArray*)returnArray;
-(NSArray *) returnArray;
*/
#property (nonatomic, strong) NSArray *returnArray;
#end
ClassB.m - no change
ViewController.h - no change
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//Create instance of ClassB
ClassB *classB = [ClassB new];
//Access the instance of returnArray
NSArray *location = [classB returnArray];
NSLog (#"The count of the location is %d", [location count]);
NSLog (#"The second item in testArray is %#", location[1]);
}
#property is a shorthand notation for creating an instance variable and associated accessor methods (with defined access / modification criteria).
What you have is a class method, which internally creates an array and returns it.
That's why you call [ClassB returnArray]; instead of [instanceOfB array];.
These are completely different things. If you wanted to use a property then you would need to create an instance of ClassB and then access the property. This would work, assuming that the array was created when the instance of ClassB was created.
Wain's answer addresses the difference between #property and Class methods, so it's worth a read. My answer assumes you know the difference between class and instance methods, and focuses on the difference between creating a #property versus creating an instance variable with an associate setter and getter.
The reason is because returnArray is a public method that returns an NSArray object on your ClassB.
A #property is merely a convenient way of creating three things at the same time: an instance variable, a setter, and a getter. It has the added bonus of allowing dot-syntax.
But at the end of the day, dot-syntax aside, all you're doing by declaring a #property is equivalently equal to this:
#interface ClassB : NSObject {
#private
NSArray *returnArray;
}
- (void)setReturnArray:(NSArray*)returnArray;
- (NSArray*)returnArray;
This is the same as this:
#property NSArray *returnArray;
Except of course, the dot syntax.
When you do:
NSArray *myArray = classB.returnArray;
You're not actually directly accessing the array you created when you declared the #property.
What you're doing is calling the getter method that was automatically generated when you declared the #property.

How can I access a value in a class which was set in another class in objective c?

I have two classes, classA and classB.
I navigated from classA to classB.
I set a value
Temp = 0; in classB.
I popped back to classA from classB.
Now, I need to access Temp value in classA.
how can I get that without setting the value in AppDelegate calss ?
You should use a Property. Have a look at the apple tutorial
#interface MyClass : NSObject
{
NSString *value;
}
#property(copy, readwrite) NSString *value;
#end
#implementation MyClass
#synthesize value;
#end
In another class that has a instance of MyClass
myInstanceOfMyClass.value = #"hi"; // Sets the value to 'hi'
NSString* myString = myInstanceOfMyClass.value; // Gets the value :)
I guess, there are two ways to do this:
Create owner object in class B and initialize it with class A's object. When done with class B, set the value in class A's variable and then use it into class A
Create a property in App Delegate and assign this variable from Class B. When back to class A, use the variable from App Delegate.