Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-16-2010, 07:21 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default Passing NSMutableArray to another object

I'm having a problem passing an NSMutableArray to another object. I have a UITabBar set up, my first view is trying to pass an array to my second view. My second view will hold a history of everything that happened in my first view.

Here's what I initially did. I created an array in my first view and in my second view. it works to send over the entire array as a pointer to the second view, but if I try to add objects to the already existing array, it doesn't work. I don't get any errors, it just keeps a count of zero objects. Here's my code.

Code:
- (void)resultFieldHasAnswer:(FirstViewController *)resultField{

	for (int i = 0; i < [resultField.array count]; i++) {
		[self.historyArray addObject:[resultField.array objectAtIndex:i]];
	}
	
}
If I pass in an NSString to another NSString in this same method, it gets passed in with no problem, but I can't figure out why its not passing each object into the new array.

I want to be able to save the history and add on to it when the user opens the app on the next launch. Please give me some inside on what I'm doing wrong, and suggestions on how to do it right. Thanks in advance.
__________________
Eliseo C

Last edited by Jrman52; 07-16-2010 at 07:49 PM.
Jrman52 is offline   Reply With Quote
Old 07-16-2010, 07:31 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Jrman52 View Post
I'm having a problem passing an NSMutableArray to another object. I have a UITabBar set up, my first view is trying to pass an array to my second view. My second view will hold a history of everything that happened in my first view.

Here's what I initially did. I created an array in my first view and in my second view. it works to send over the entire array as a pointer to the second view, but if I try to add objects to the already existing array, it doesn't work. I don't get any errors, it just keeps a count of zero objects. Here's my code.

Code:
- (void)resultFieldHasAnswer:(FirstViewController *)resultField{

	for (int i = 0; i < [resultField.test count]; i++) {
		[self.historyArray addObject:[resultField.array objectAtIndex:i]];
	}
	
}
If I pass in an NSString to another NSString in this same method, it gets passed in with no problem, but I can't figure out why its not passing each object into the new array.

I want to be able to save the history and add on to it when the user opens the app on the next launch. Please give me some inside on what I'm doing wrong, and suggestions on how to do it right. Thanks in advance.
Eliseo,

Are you getting a compile error or a runtime error? And what is that error?

My guess is that your self.historyArray is an NSArray, not an NSMutableArray.

NSArray objects have a fixed set of members, and you can't add, remove, or change the order of the objects within once you create the array. NSMutableArray objects have methods like addObject, removeObject, and sortUsingComparator that let you change the contents and order of the array.

If my guess is wrong you'll have to provide more information.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-16-2010, 07:44 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Eliseo,

Are you getting a compile error or a runtime error? And what is that error?

My guess is that your self.historyArray is an NSArray, not an NSMutableArray.

NSArray objects have a fixed set of members, and you can't add, remove, or change the order of the objects within once you create the array. NSMutableArray objects have methods like addObject, removeObject, and sortUsingComparator that let you change the contents and order of the array.

If my guess is wrong you'll have to provide more information.
Hi Duncan! Both Arrays are NSMutableArrays. Here's a sample of what's on my first view...

Code:
@protocol FirstViewControllerDelegate;

@interface FirstViewController : UIViewController {
	id <FirstViewControllerDelegate> delegate;
	
	IBOutlet UILabel *resultField;
	IBOutlet UILabel *equationField;
	NSMutableArray *array; 

}
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UILabel *resultField;
@property (nonatomic, retain) NSMutableArray *array;

- (IBAction)calculateResult;

@end

@protocol FirstViewControllerDelegate

- (void)resultFieldHasAnswer:(FirstViewController *)resultField;

@end

And then here's what's in my second view...

Code:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"


@interface SecondViewController : UIViewController <FirstViewControllerDelegate, UITableViewDelegate> {
	
	NSMutableArray *historyArray;

}

@property (nonatomic, retain) NSString *history;
@property (nonatomic, retain) NSMutableArray *historyArray;

@end
I alloc and initialized historyArray in my viewDidLoad method. If I pass the entire array from first view to second view, np. But I want add each object individually into the second view array since I'm going to be storing the history in there. I want to be able to keep adding to it until the user decides to clear the history. I'm not sure If I gave you enough information, hopefully I did, but please feel free to ask me for any further details. BTW, I don't get any errors at all, compiles perfectly.
__________________
Eliseo C

Last edited by Jrman52; 07-16-2010 at 07:52 PM.
Jrman52 is offline   Reply With Quote
Old 07-16-2010, 07:44 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Put an NSLog at the beginning of that method to log the result of [resultField.test count]. That will help you twofold; first, it'll show you if the method is even being called, and second, it'll show you if the for loop isn't ever running because [resultField.test count] is 0 or something.

Then, after the for loop, put another NSLog to log the description of the historyArray. That'll show you whether your problem is reading from the array or writing to the array.

If that doesn't help you find the answer, post the results here for us and maybe we can help you more.
SoulRed12 is offline   Reply With Quote
Old 07-16-2010, 07:48 PM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
Put an NSLog at the beginning of that method to log the result of [resultField.test count]. That will help you twofold; first, it'll show you if the method is even being called, and second, it'll show you if the for loop isn't ever running because [resultField.test count] is 0 or something.

Then, after the for loop, put another NSLog to log the description of the historyArray. That'll show you whether your problem is reading from the array or writing to the array.

If that doesn't help you find the answer, post the results here for us and maybe we can help you more.
Hi SoulRed. I actually tried doing that. The NSLogs get called, but the count is 0 and the object is null. I even tried adding just one object when alloc and initializing the NSMutable array, but even that resulted a count of 0 and null. I tried adding an NSString, but same thing, count is 0, object is null. No compile errors at all.

Just to Note, I entered the code wrong in this thread, its "resultField.array". The code in my project has all the naming correct.
__________________
Eliseo C

Last edited by Jrman52; 07-16-2010 at 07:53 PM.
Jrman52 is offline   Reply With Quote
Old 07-16-2010, 07:53 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

So [resultField.test count] returns 0? Interesting...that seems to suggest there's something wrong with your first view controller.

One thing I do notice is that you've got [resultField.array objectAtIndex:i] inside your for loop, but you've only got an ivar for NSMutableArray *test. Could it be that you're accessing the wrong NSMutableArray? Perhaps I'm misunderstanding your code, but that looks like a possibility.
SoulRed12 is offline   Reply With Quote
Old 07-16-2010, 07:57 PM   #7 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
So [resultField.test count] returns 0? Interesting...that seems to suggest there's something wrong with your first view controller.

One thing I do notice is that you've got [resultField.array objectAtIndex:i] inside your for loop, but you've only got an ivar for NSMutableArray *test. Could it be that you're accessing the wrong NSMutableArray? Perhaps I'm misunderstanding your code, but that looks like a possibility.
Yea, I'm sorry about that. I entered it wrong here. Its actually as follows...

Code:
#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate;

@interface FirstViewController : UIViewController {
	id <FirstViewControllerDelegate> delegate;
	
	IBOutlet UILabel *resultField;
	IBOutlet UILabel *equationField;
	NSMutableArray *array; 

}
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UILabel *resultField;
@property (nonatomic, retain) NSMutableArray *array;

- (IBAction)calculateResult;

@end

@protocol FirstViewControllerDelegate

- (void)resultFieldHasAnswer:(FirstViewController *)resultField;

@end
And the code in my second view is like this...

Code:
- (void)resultFieldHasAnswer:(FirstViewController *)resultField{
	
	for (int i = 0; i < [resultField.array count]; i++) {
		[self.historyArray addObject:[resultField.array objectAtIndex:i]];
	}
	
	NSLog(@"historyArray number of items %d", [self.historyArray count]);
}
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-16-2010, 08:02 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Jrman52 View Post
Hi Duncan! Both Arrays are NSMutableArrays. Here's a sample of what's on my first view...

Code:
@protocol FirstViewControllerDelegate;

@interface FirstViewController : UIViewController {
	id <FirstViewControllerDelegate> delegate;
	
	IBOutlet UILabel *resultField;
	IBOutlet UILabel *equationField;
	NSMutableArray *array; 

}
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UILabel *resultField;
@property (nonatomic, retain) NSMutableArray *array;

- (IBAction)calculateResult;

@end

@protocol FirstViewControllerDelegate

- (void)resultFieldHasAnswer:(FirstViewController *)resultField;

@end

And then here's what's in my second view...

Code:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"


@interface SecondViewController : UIViewController <FirstViewControllerDelegate, UITableViewDelegate> {
	
	NSMutableArray *historyArray;

}

@property (nonatomic, retain) NSString *history;
@property (nonatomic, retain) NSMutableArray *historyArray;

@end
I alloc and initialized historyArray in my viewDidLoad method. If I pass the entire array from first view to second view, np. But I want add each object individually into the second view array since I'm going to be storing the history in there. I want to be able to keep adding to it until the user decides to clear the history. I'm not sure If I gave you enough information, hopefully I did, but please feel free to ask me for any further details. BTW, I don't get any errors at all, compiles perfectly.
Wait a minute. Something doesn't make sense. In your previous post you said the problem code was:


Code:
- (void)resultFieldHasAnswer:(FirstViewController *)resultField{

	for (int i = 0; i < [resultField.array count]; i++) {
		[self.historyArray addObject:[resultField.array objectAtIndex:i]];
	}
}
But in the headers you just posted, resultField is a UILabel. UILabels don't have a resultField property. Oh, wait. your resultFieldHasAnswer method uses the name "resultField as a parameter for a FirstViewController object. That's really, really confusing. I would strongly suggest against that, especially since your FirstViewController object has a property called resultField. That would lead to such ugly code as resultField.resultField.

Anyway, I would add tons of debug statements:



Code:
- (void)resultFieldHasAnswer:(FirstViewController *)otherViewController; //Name changed
{
  NSLog("@On entry, FirstViewController = %@", otherViewController); 
  NSLog("@otherViewController.array = %@", otherViewController.array);
  NSLog("@[otherViewController.array count] = %@", [otherViewController.array count]);

  for (int i = 0; i < [otherViewController.array count]; i++) 
  {
    [self.historyArray addObject:[otherViewController.array objectAtIndex:i]];
  }
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-16-2010, 08:07 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Alright. Well since historyArray is a property, why don't you just use

Code:
- (void)resultFieldHasAnswer:(FirstViewController *)resultField{
	historyArray = [NSMutableArray arrayWithArray:resultField.array];
}
It'll automatically take care of the memory management through the fact that it's a property. In addition, you're going to get the same result as adding them all in one-by-one, because you'd be just adding pointers to the same objects that were in resultField.array (literally, the same objects; when you add an object to an array it only stores a pointer to that object and increments its retain count by 1).

If you wanted to actually copy the objects, so the historyArray indexes pointed to objects that were separate copies of the objects in resultField.array, you'd have to specifically copy them using the copy method before inserting them into the array.

Nevertheless, I'm still unsure why it's not working the way you have it, and I'm beginning to suspect more and more that the problem is elsewhere in the code. Try using the code I wrote above just as a test (even if it's not what you want for your app) and see if it works. At the very least it'll provide some more evidence to find the problem.

Last edited by SoulRed12; 07-16-2010 at 08:09 PM.
SoulRed12 is offline   Reply With Quote
Old 07-16-2010, 08:34 PM   #10 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Okay, i tried your suggestions. When i test the array in the first view, all the objects are recorded and added. But once I check i in here...

Code:
- (void)resultFieldHasAnswer:(FirstViewController *)firstViewController{
	NSLog(@"inside resultFieldHasAnswer");
	
	NSLog(@"On entry, FirstViewController = %@", firstViewController); 
	NSLog(@"firstViewController = %@", firstViewController.array);
	NSLog(@"[firstViewController.array count] = %@", [firstViewController.array count]);
	
	for (int i = 0; i < [firstViewController.array count]; i++) {
		[self.historyArray addObject:[firstViewController.array objectAtIndex:i]];
	}
	
	NSLog(@"historyArray number of items %d", [self.historyArray count]);
}
It crashes on NSLog(@"[firstViewController.array count] = %@", [firstViewController.array count]);

I tried using a break point and stepping into the problem, but when it goes into the array it crashes and gives me a bad access. I would guess that would be a memory problem, but how does it get lost in memory if I never release it.
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-16-2010, 08:46 PM   #11 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

I tried that, but its replacing my old array with the new array. So its not working. Any other suggestions?
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-16-2010, 08:57 PM   #12 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Jrman52 View Post
Okay, i tried your suggestions. When i test the array in the first view, all the objects are recorded and added. But once I check i in here...

Code:
- (void)resultFieldHasAnswer:(FirstViewController *)firstViewController{
	NSLog(@"inside resultFieldHasAnswer");
	
	NSLog(@"On entry, FirstViewController = %@", firstViewController); 
	NSLog(@"firstViewController.array = %@", firstViewController.array);
	NSLog(@"[firstViewController.array count] = %d", [firstViewController.array count]);
	
	for (int i = 0; i < [firstViewController.array count]; i++) {
		[self.historyArray addObject:[firstViewController.array objectAtIndex:i]];
	}
	
	NSLog(@"historyArray number of items %d", [self.historyArray count]);
}
It crashes on NSLog(@"[firstViewController.array count] = %@", [firstViewController.array count]);

I tried using a break point and stepping into the problem, but when it goes into the array it crashes and gives me a bad access. I would guess that would be a memory problem, but how does it get lost in memory if I never release it.
Oh crap. Sorry about that. You need to display a count value using %d, the format string for an integer. The log statements I posted used %@, which is the format string for an object. I fixed it in the code above, and marked the offending line in bold.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 07-16-2010 at 09:01 PM.
Duncan C is offline   Reply With Quote
Old 07-16-2010, 09:00 PM   #13 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

(I just realized what you're trying to do; the array in the first view controller contains the new user activity and you want to add that to the array that the second view controller already owns, right? I assumed the first view controller's array held all the history data total, and you were just trying to get the pointers in the first array to a separate second array)

EDIT: Just saw Duncan's post, now I understand why the crash happened. Sorry.

It seems the problem would most likely in either the where the first view controller sends over the array, or where the second view controller creates its array. Perhaps posting a bit of that would be helpful? I don't want to ask you to reveal code you want to keep private, but at least consider it so we can have more information about what you're specifically doing.

Last edited by SoulRed12; 07-16-2010 at 09:08 PM.
SoulRed12 is offline   Reply With Quote
Old 07-17-2010, 04:06 AM   #14 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Okay, so I've reworked this a little. Let me show you what I have, then I'll explain...

FirstViewController.h
Code:
#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate;

@interface FirstViewController : UIViewController {
	id <FirstViewControllerDelegate> delegate;
	
	IBOutlet UILabel *resultField;
	NSString *history;

}
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UILabel *resultField;
@property (nonatomic, retain) NSString *history;

- (IBAction)calculateResult;

@end

@protocol FirstViewControllerDelegate

- (void)firstViewController:(FirstViewController *)firstViewController hasAnswer:(NSString *)answer;

@end
FirstViewController.m
Code:
#import "FirstViewController.h"


@implementation FirstViewController

@synthesize delegate;
@synthesize resultField;
@synthesize history;


- (IBAction)userInput:(id)sender{
	int senderTag = [sender tag];
	self.resultField.text = [NSString stringWithFormat:@"%d", senderTag];
}//end userInput

- (IBAction)calculateResult{
	NSLog(@"Inside CalculateResult Method");
	self.history = self.resultField.text;
	[self.delegate firstViewController:self hasAnswer:self.history];
}//end calculateResult


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
 
	history = [[NSString alloc] init];
}

@end
SecondViewController.h
Code:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"


@interface SecondViewController : UIViewController <FirstViewControllerDelegate, UITableViewDelegate> {
	
	NSMutableArray *historyArray;

}

@property (nonatomic, retain) NSMutableArray *historyArray;

@end
SecondViewController.m
Code:
#import "SecondViewController.h"


@implementation SecondViewController

@synthesize historyArray;

- (void)firstViewController:(FirstViewController *)firstViewController hasAnswer:(NSString *)answer{
	
        //This actually works and shows me that temp is not nil or invalid
	NSString *temp = [NSString stringWithFormat:@"%@", answer];
	NSLog(@"temp = %@", temp);
	
        //Add new temp NSString to historyArray
	[self.historyArray addObject:temp];
	
        //Problem : Prints null 
        for(int i = 0; i < [self.historyArray count]; i++)
       {
              NSLog(@"historyArray object at index %d is %@", i, [self.historyArray objectAtIndex:i]);
        }

        //Problem : Prints that 0 objects are in array
	NSLog(@"historyArray number of items %d", [self.historyArray count]);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [self.historyArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell;
	cell= [tableView dequeueReusableCellWithIdentifier:@"History Cell"];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"History Cell"] autorelease];
	}
	
	cell.textLabel.text = [self.historyArray objectAtIndex:indexPath.row];
	
	return cell;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	historyArray = [[NSMutableArray alloc] init]; 
}

//....other methods unrelated to problem  here....

@end
So that's my code for both view controllers. So what I'm trying to do is add a NSString to the "historyArray". But every time I try to do that, it shows me that nothing is added to the object. If I do the same array code in the FirstViewController, just to test if adding an object to an array works, everything works like a charm. But I need the SecondViewController to keep track of everything that's going to display in its UITableView History.

When I ran the code, I can see that the NSString is passed on successfully. But never added to the array. I even tried this in the SecondViewController

Code:
self.historyArray = [[NSMutableArray alloc] initWithObjects:@"Test", @"Test2", nil];
And when I checked the array, it had a count of 0. No errors, no nothing, everything is compiled without a problem. I'm at a lost on what I'm doing wrong. Should I alloc and init the string, add it to the object, then release it? This is driving me nuts.
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-17-2010, 01:27 PM   #15 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Okay I figured out the problem. Since viewDidLoad is not called on the "SecondViewController" until someone clicks on the tab bar for it, I had to do the following...

Code:
- (void)firstViewController:(FirstViewController *)firstViewController hasAnswer:(NSString *)answer{

	NSString *temp = [NSString stringWithFormat:@"%@", answer];

        if(self.historyArray == nil){
           self.historyArray = [[NSMutableArray alloc] init];
        }	

	[self.historyArray addObject:temp];       
}
So here's my other problem...lol. And hopefully is the last thing I ask of you great people. How do I save my data? I tried looking thru the forums but they don't have a straight out answer to what i'm looking for. I wanted to know how to save data using either "applicationWillTerminate" or "applicationDidEnterBackground".

Data gets saved in my "SecondViewController" even if the user never loads the view. How do I tell it to save it when it terminates. If you have to direct me to another post that I may have missed that would be grateful. I again, thank you guys for the help.
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-17-2010, 03:23 PM   #16 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Try NSUserDefaults. You can save entire arrays with it, as long as the arrays only contain NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary objects. Since your array will be containing NSString's, you can save it using setObject:forKey: and load it again with arrayForKey:. Note that when you use arrayForKey, the returned array will be immutable, which means you're going to have to do something like self.historyArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardDefaults] arrayForKey:@"savedHistoryArray"]] instead of just using the NSArray object returned by arrayForKey.
SoulRed12 is offline   Reply With Quote
Old 07-17-2010, 04:20 PM   #17 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
Try NSUserDefaults. You can save entire arrays with it, as long as the arrays only contain NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary objects. Since your array will be containing NSString's, you can save it using setObject:forKey: and load it again with arrayForKey:. Note that when you use arrayForKey, the returned array will be immutable, which means you're going to have to do something like self.historyArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardDefaults] arrayForKey:@"savedHistoryArray"]] instead of just using the NSArray object returned by arrayForKey.
Hi Soul, thanks again for replying. I actually did this...

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	history = [[NSMutableArray alloc] init];
	
	NSString *temp = [[NSUserDefaults standardUserDefaults] valueForKey:@"History"];
	
	[history addObjectsFromArray:[temp componentsSeparatedByString:@", "]];
}

- (void)saveHistory{
	if ([history count] > 0) {
		NSString *temp = [history componentsJoinedByString:@", "];
		[[NSUserDefaults standardUserDefaults] setValue:temp forKey:@"History"];
	}
}
Here's my issue though. How do I get it to save when the app terminates. I decided to just save all the history in my FirstViewController, but when the application quits, the "viewdidunload" isn't called. How can I tell my application delegate to save the information on "applicationWillTerminate".

My ViewControllers are set up thru IB, so my applicationDelegate doesn't have any outlets to it. Should I create these outlets? Will that be easier to do, or is there a simpler way? This is my first time saving data like this, so tell it to me like it is...lol. Thanks a million.
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-17-2010, 04:52 PM   #18 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

You can always reach your applicationDelegate by calling [UIApplication sharedApplication].delegate. So what you can do, is set up an ivar and property on your app delegate, and when your history view controller loads, it should set the property correctly by calling something like (MyAppAppDelegate*)[UIApplication sharedApplication].delegate.historyArrayToSave = historyArray;

Then your app delegate will have a pointer to the history array, and you can save it in the appropriate methods. Remember to #import your App Delegate's header file in your history view controller's .m file. Also, the cast is there so xCode doesn't throw an error for not recognizing historyArrayToSave (or whatever you decide to call it).

I'm pretty sure this should work, I don't see why it wouldn't. Good luck!
SoulRed12 is offline   Reply With Quote
Old 07-17-2010, 06:32 PM   #19 (permalink)
Registered Member
 
Join Date: May 2010
Location: Los Angeles, Ca
Posts: 27
Jrman52 is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
You can always reach your applicationDelegate by calling [UIApplication sharedApplication].delegate. So what you can do, is set up an ivar and property on your app delegate, and when your history view controller loads, it should set the property correctly by calling something like (MyAppAppDelegate*)[UIApplication sharedApplication].delegate.historyArrayToSave = historyArray;

Then your app delegate will have a pointer to the history array, and you can save it in the appropriate methods. Remember to #import your App Delegate's header file in your history view controller's .m file. Also, the cast is there so xCode doesn't throw an error for not recognizing historyArrayToSave (or whatever you decide to call it).

I'm pretty sure this should work, I don't see why it wouldn't. Good luck!
THANKS!
__________________
Eliseo C
Jrman52 is offline   Reply With Quote
Old 07-17-2010, 07:03 PM   #20 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Boston, MA
Posts: 135
carbonbasednerd is on a distinguished road
Default

You should make note that with iOS4 saving to user defaults is not guaranteed when the application is terminated. Proper termination, not fast-app switching suspend. The app just gets a kill signal. You should synchronize your NSUserdefaults at that time in order to make sure everything is saved when applicationWillTerminate is called.
carbonbasednerd is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, nsarray, nsmutablearray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
14 members and 317 guests
akphyo, alexP, appservice, cgokey, EXOPTENDAELAX, flamingliquid, guusleijsten, mariano_donati, ohmniac, Paul Slocum, PavelSea, Rudy, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0