To add an class object in nsmuttable array of another class - xcode

I have two classes results and flight segment
Result
{
// Here some properties declared and
// Nsmuttable array also to store flight segment class object
}
I create object of both classes and adding in this way
[objresult.flightsegmentarray addobject: objflightsegment]
But this doesn't work

You should ensure you have properly declared the 'flightsegmentarray' property on the 'objresult' object and have initialised it before trying to add objects.
To declare the property - assuming your objresult is of type ObjResult - you should write on your ObjResult .h file:
#property (nonatomic, strong) NSMutableArray *flightsegmentarray;
Then, you must initialise this, typically in your .m file:
- (id)init {
if ((self=[super init])) {
_flightsegmentarray = [NSMutableArray new];
}
return self;
}
Now, once your objresult object has been initialised, you will have an empty mutable array available and you can begin adding objects:
objresult = [[ObjResult alloc] init];
objflightsegment = [[ObjFlightSegment alloc] init];
[objresult.flightsegmentarray addobject: objflightsegment];

Related

Key value Observer is not getting called when there is a change in property in cocoa

I had 2 Objects Obj1 and Obj2. Where Obj1 is having a property isLocalChanges(BOOL) to check for any local changes made. I need to observe the change for this isLocalChanges if there is any changes I am setting the value to YES.
I written an observer for this property isLocalChanges in Obj2:
#interface Obj2 : NSObject
{
Obj1 *sampleObj;
}
#property (nonatomic,retain) Obj1 * sampleObj;
#end
#implementation Obj2
#synthesize sampleObj;
- (id)init
{
if (self = [super init])
{
sampleObj = [[Obj1 alloc] init];
[sampleObj addObserver:self
forKeyPath:#"isLocalChanges"
options:NSKeyValueObservingOptionNew
context:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"isLocalChanges"]) {
NSLog(#"isLocalChanges Observer");
if ([[change objectForKey:#"new"] boolValue]) {
NSLog("#\n Found Local Changes...");
}
}
}
}
Declaration and usage of isLocalChanges property that I used:
#interface Obj1 : NSObject
{
BOOL _isLocalChanges;
}
#property (assign) BOOL isLocalChanges;
#end
and in the Obj1.m
did #synthesize isLocalChanges = _isLocalChanges;
And Inside the below method I am setting the value for isLocalChanges property.
-(void) localChangesMade
{
self.isLocalChanges = YES;
}
The issue is even if isLocalChanges property changed in Obj1, The method observeValueForKeyPath: is not triggering. At this point I am helpless an unable to find what going wrong.
Can anyone suggest me to resolve this issue...
The object you are allocating is going out-of-scope and will be destroyed as soon as the method returns:
SomeObject *Obj2 = [[SomeObject alloc] init];
Make it an instance variable instead.
You are using the identifier Obj2 for three different things (a) as a class name (b) as an instance variable/property and (c) as a local variable. This is not good.
It is probably the third use which is your immediate problem, it is probably meant to be a use of (b). Instead of:
if (self = [super init])
{
SomeObject *Obj2 = [[SomeObject alloc] init];
you probably want:
if (self = [super init])
{
Obj2 = [[SomeObject alloc] init];
etc. The SomeObject *Obj2 local variable's lifetime is only the body of the if.
However that fix has you assigning an instance of type SomeObject to a property of type Obj1, so you may have a problem there as well.
Finally you never show the declaration or use of isLocalChanges, so there is no indication that you're actually assigning to this property using its setter - and if you're not then there won't be any KVO notifications.

ARC Creating New Objects in Method

I just moved a project from MRR to ARC using Xcode's tool. I have a routine that works like this:
#interface myObject
{
NSMutableArray* __strong myItems;
}
#property NSMutableArray* myItems;
- (BOOL) readLegacyFormatItems;
#end
- (BOOL) readLegacyFormatItems
{
NSMutableArray* localCopyOfMyItems = [[NSMutableArray alloc]init];
//create objects and store them to localCopyOfMyItems
[self setMyItems: localCopyOfMyItems]
return TRUE;
}
This worked fine under MRR, but under ARC myItems is immediately released. How can I correct this?
I've read about __strong and __weak references, but I don't yet see how to apply them in this case.
Thanks very much in advance to all for any info!
This should work, as it is. But you don't need to declare the iVars anymore. Just use properties. You even don't need to synthesize them. Strong properties will retain any assigned object, weak properties won't.
Also class names should always be uppercase. And - since you store a mutable array - you can also add your objects directly to the property. No need for another local mutable array variable.
#interface MyObject
#property (nonatomic, strong) NSMutableArray *myItems;
- (BOOL)readLegacyFormatItems;
#end
#implementation MyObject
- (BOOL) readLegacyFormatItems
{
self.myItems = [[NSMutableArray alloc]init];
//create objects and store them directly to self.myItems
return TRUE;
}
#end

NSMutableDictionary setObject:forKey - custom class

I am using this in a UINavigation environment.
I have customClassA. It inherits customClassB and one of its object is a NSMutableDictionary.
I alloc and init customClassA in a viewController, then for adding data, I am pushing a new viewController into the stack. The addNewDataViewController sends the newly added data, a customClassB object back by its delegate. Everything works fine so far.
customClassA has to store the returned object (customClassB) into its NSMutableDictionary object with a key (an NSString created from NSDate).
I get "mutating method sent to immutable object" error and can't think of any solution.
Any help is appreciated.
Thanks.
===========================
interface customClassA : NSObject
{
NSDate date;
NSArray *array; // will contain only NSString objects
}
// and the rest as customary
...
#import "customClassA.h"
interface customClassB : NSObject
{
NSString *title;
NSMutableDictionary *data; // will contain values of customClassA with keys of NSString
}
// and the rest as customary
...
#import "customClassB"
#interface firstViewController : UITableViewController <SecondViewControllerDelegate>
- (void)viewDidLoad
{
self.customClassB_Object = [customClassB alloc] init];
// and the rest...
}
- (void)secondViewControllerDidSaveData:(customClassA *)aData
{
[self.customClassB_Object.data setObject:aData forKey:[NSString stringWithFormat:#"%#", aData.date]];
// update tableView
}
Make sure you are initializing the NSMutableDictionary with something like
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
It would appear that your NSMutableDictionary is getting created with an NSDictionary instance instead of a NSMutableDictionary
Althoguh I added the following code to customClassB implementation, it still didn't work.
#implementation customClassB
- (id)init
{
self = [super init];
if (self)
self.data = [NSMutableDictionary alloc] init];
return self;
}
so I added two custom methods to my customClassB implementation, as well as in the header file:
- (void)appendData:(customClassA *)aData;
- (void)removeDataWithKey:(NSString *)aKey;
and instead of manipulating the data dicionary of customClassB in my viewController, I simply call that method and pass the data object to the class and it did the trick.

initWithDictionary

I have the problem that I can't get the Data from one of my classes to the other...
To do this I created this method in the class I am initializing (angebotPage):
- (id) initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
dict = [dictionary retain];
}
return self;
}
The call from the other class looks like this:
angebotPage *page;
angebotPDF = [[PDFDocument alloc] init];
page = [[angebotPage alloc] initWithDictionary:dictionary];
The Error I get is EXC_BAD_ACCESS in the line where I do:
dict = [dictionary retain];
But why? I need to retain it cause I will use it for the next program steps.. But without retaining I can't use it (EXC_BAD_ACCESS comes elsewhere...)
I recommend that you use a property for dict instead.
#property (retain) NSDictionary *dict;
And then assign the dictionary with self.dict = dictionary. This is assuming all the objects in dictionary are correctly retained in the first place. Try with an empty dictionary if in doubt.

How to implement key value observing of objects in an NSMutableArray

I need some help trying to understand KVO on a complex hierarchy of objects. Let me set the scenario. The MyClass object has a mutable array property that contains MyPerson objects. I want to observe changes in the myPeople property of MyClass. Furthermore I would like to observe all the properties contained in the MyPerson object as well. Here are the class definitions.
#interface MyClass:NSObject
{
NSMutableArray *myPeople;
}
#property(nonatomic, retain)NSMutableArray *myArray;
#end
Here is the MyPerson object,
#interface MyPerson:NSObject
{
NSString *myName;
NSString *myLastName;
}
#property(nonatomic, retain)NSString *myName;
#property(nonatomic, retain)NSString *myLastName;
#end
Is it correct to observe the properties that I'm interested in the following manner?
MyClass *myClass = [[MyClass alloc] init]; //myPeople is filled with myPerson objects
MySchool *mySchool = [[MySchool alloc] init];
[myClass addObserver:mySchool
forKeyPath:#"myPeople"
options:NSKeyValueObservingOptionNew
context:NULL];
[myClass addObserver:mySchool
forKeyPath:#"myPeople.myName"
options:NSKeyValueObservingOptionNew
context:NULL]; //I am unsure about this one
[myClass addObserver:mySchool
forKeyPath:#"myPeople.myLastName"
options:NSKeyValueObservingOptionNew
context:NULL]; //I am unsure about this one
No, it's not correct. You would have to observe the properties for any object you add to the array separately. So whenever an object is added to or removed from the array, you would have to add/remove your observer to/from the added/removed object(s).

Resources