views:

39

answers:

3

Hi,

Hopefully I can make this clear, but I am new to Objective-C and to be honest not great with Arrays in the first place.

So, I have a Singleton class (called SingletonState) that I am using to pass variables around my app (please can we leave the whether I should use Singleton classes out of this - I will fix that later). In this class, I have an NSMutableArray (called arrayMyEvents). I have also created a class that I am going to store a list of events (called EventClass). When the user logs in, I call a web service and get back 3 strings. The 3rd string is a comma separated list of value. I parse the data and populate the custom class EventClass. I then add this class to the SingletonState.arrayMyEvents.

I have all of this working. I can go to another ViewController and access the "count" of items in arrayMyEvents.

PROBLEM: I now want to edit one of the ScheduledEventsClass"es" in my array. How do I get access to it and edit some of the properties and update the one in my SingletonState class?

Here is some of the code, that I've tried:

NSString *sWebServiceEvents = [[NSString alloc] initWithFormat:@"%@", [result objectAtIndex:2]];

            if ( [ sWebServiceEvents isEqualToString:@"NULL" ] != true ) {

                NSArray *arrayEvents = [sWebServiceEvents componentsSeparatedByString:@","];

                // If the array has not been initialized they initialize it.
                if (sharedState.arrayMyEvents == nil) {
                    sharedState.arrayMyEvents = [[NSMutableArray alloc ] init ];
                }


                for (NSString * sEvent in arrayEvents) {
                    // Set equal to the value of the array (the Event Number) at the same
                    // position as the row that we are being asked to return a cell/row for.
                    EventClass *eventClass = [[EventClass alloc] retain];

                    eventClass.sEvent = sEvent;

                    [ sharedState.arrayEvents addObject:eventClass ];

                }

                NSLog(@"LoginView - sharedState.arrayMyEvents Count: %d", [sharedState.arrayMyEvents count]);
            }

Here is me trying to access it in another ViewController:

EventClass *eventClass =
[sharedState.arrayMyEvents objectAtIndex:row ];
NSLog(@"eventClass.sEventNumber: ", eventClass.sEventNumber);
eventClass.sLocation = @"Jason's Big Location";
+1  A: 

You're going to have some memory leaks from the sEvent loop. [[EventClass alloc]retain] leaves you an uninitialized EventClass object with a reference count of 2. You'll need to change that to [[[EventClass alloc] init] autorelease] to keep it from leaking. The arrayEvents NSMutableArray will retain it during the addObject: call. (Shouldn't that be [sharedState.arrayMyEvents addObject: eventClass] in the loop?)

After that, all you have to do to edit the EventClass object in the second block of code is edit it. The eventClass variable is a pointer to an object in the array. Anything done to that object doesn't affect the pointer referencing it, it affects data referenced by it. The code you have in the second block should change the sLocation of the selected object as you intend.

You have a few more memory leaks in there, too. Use Cmd-Shift-A to build with the static analyzer and it'll tell you where.

John Franklin
Hi John, first, thank you very much for taking the time to help me solve this. It is appreciated!!!I will be checking into the memory leaks. Getting an understanding of memory management is close to next on my list of things (long list with iPhone Dev).I think that I figured out my problem.See below in the answer area, as I ran out of space in the comment area.thanks again!!!Jason
JasonBub
A: 

Maybe the problem is that you put them in sharedState.arrayEvents but try to take them out of sharedState.arrayMyEvents. Different variables.

Also, lots of memory leaks.

St3fan
Hi St3fan,thanks for posting a response! I believe that you might be responding to a typo in my post code. I edited the variable names to not expose my employers product and must of messed a few up.I believe that I figured out the issue. I posted about it below.Thanks again for your time!Jason
JasonBub
A: 

Hi,

Thanks John and St3fan, your answers and time are appreciated!!!

I think that I figured out my issue:

Basically, the class that I created (EventClass) had the properties setup like this:

@property (nonatomic, assign) NSString *sStudyNumber;   
@property (nonatomic, assign) NSString *sTheater;

but, they should be (or at least I got it to work like this):

@property (nonatomic, retain) NSString *sStudyNumber;   
@property (nonatomic, retain) NSString *sTheater;

Then, in my second view I was able to do this:

EventClass *eventClass = [sharedState.arrayMyEvents objectAtIndex:row ];
NSLog(@"MyEvents: %@", eventClass.sEventNumber);
    eventClass.sLocation = @"Jason's Big Location";

I then checked it in another method of the view using this and it was still there:

EventClass *eventClass = [sharedState.arrayMyEvents objectAtIndex:row ];
NSLog(@"MyEvents: %@", eventClass.sEventNumber);
NSLog(@"MyEvents: %@", eventClass.sLocation);

I also, checked it in yet another view and the value was maintained in the SharedState.arrayMyEvents without issue. :)

In the end, I believe that I boiled down to the difference between "assign" and "retain".

Now, on to the memory leaks :(

Please, let me know if you see any other issues with this.

Thanks, Jason

JasonBub
Yup, that would do it, too. The assign vs retain usage also falls under memory management, which is a tricky concept in Objective-C. Good luck with it!
John Franklin
What exactly is the difference anyway?
JasonBub
Every object has a retain count that tracks how many other objects claim to be using it. When the retain count goes to zero, no one is using it anymore, and the object is deallocated. A property with "assign" will not have the retain count affected when calling "self.foo = bar", where as a property with retain will "release" the old value and "retain" the new value. Apple has some docs on Declared Properties worth reading:http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/articles/ocProperties.html
John Franklin