PDA

View Full Version : Creating a mutable array that can be saved?




blrogers
Aug 13, 2010, 09:28 AM
Hey everyone,
I'm new to this whole thing. I'm trying to figure this whole Objective-C thing, just started last week and I'm working about 8 hours a day.
I've finished Hello World and such and now I'm on to arrays.

My goal is to create an array of strings that can be changed with UITextFields on the iPad. I wrote some code but there's an error on every single line so I didn't think posting it would be helpful at all.

Eventually I want to be able to save the array so that it remembers the changes from the last time the app was used.

Any help would be awesome,
thanks :)



robbieduncan
Aug 13, 2010, 09:29 AM
Post the code.

blrogers
Aug 13, 2010, 09:36 AM
//This is my .h file
#import <UIKit/UIKit.h>

@interface SaveInfoViewController : UIViewController {

IBOutlet UITextField *input1;
IBOutlet UITextField *input2;

IBOutlet UILabel *output1;
IBOutlet UILabel *output2;

NSMutableArray *savedArray;
}

@property (nonatomic, retain) NSMutableArray *savedArray;

-(IBAction)saveNew;
-(IBAction)printArray;

@end

//this is my .m file
#import "SaveInfoViewController.h"

@implementation SaveInfoViewController

@synthesize savedArray;

savedArray = [[NSMutableArray alloc] initWithObjects:@"you",@"me",nil];

-(IBAction)printArray {
[output1 setText:[savedArray[0] text];
[output2 setText:[savedArray[1] text]];
}

-(IBAction)saveNew {
[savedArray[0] setText:[input1 text]];
[savedArray[1] setText:[input2 text]];
}



Thats what I have

chown33
Aug 13, 2010, 10:25 AM
I see no specific reason why there should be an error on every line.
Show at least one of the errors, so we can see what is wrong.


savedArray = [[NSMutableArray alloc] initWithObjects:@"you",@"me",nil];

This is wrong. You can't have initializers that execute code.

This line needs to be moved inside a method or function body, and most likely inside the body of a -init method, which you will need in your class.

What other languages are you familiar with, if any? This line looks like something from C++ or Java, which is more lax on initializers.

All your subscripting of savedArray is completely wrong. It's an object, not a C-style array.

What book or tutorial are you using? Be specific.

PhoneyDeveloper
Aug 13, 2010, 10:30 AM
There are a lot of errors in that code. You should work through a book on iPhone development start to finish. That's the way to learn.

blrogers
Aug 13, 2010, 10:59 AM
I took a class in Java, I got pretty good at java, I would say probably 8 out of 10. I wrote some programs that were more than 8,000 lines of code.

The way objects are used in objective C is really confusing me. I'm not using any specific book or tutorial. I know this is horrible, but every time I run into a problem I just google it until I find a solution.

Couldn't find a solution so I joined this forum.
Where would I put the initialize method?

robbieduncan
Aug 13, 2010, 11:04 AM
Couldn't find a solution so I joined this forum.
Where would I put the initialize method?

Stop!
Hammer Time!

1) Program quality or the quality of the programmer is not measured in lines of code. In fact a bad programmer will often use more lines of code than a good one. I was very happy this year when I was able to delete 8000 lines of code from a (Java) project at work.

2) An init method is a bit like a constructor in Java. I'm sure you can now see why this might be helpful

I suggest you start here (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html)

Anne R. Kist
Aug 13, 2010, 11:09 AM
I know this is horrible, but every time I run into a problem I just google it until I find a solution.
At least there were no errors in the bolded statement. ;)

chown33
Aug 13, 2010, 11:43 AM
I'm not using any specific book or tutorial.

There's your problem right there.

If the only language you know is Java, and you only know it from classroom study, there is almost no chance you're going to figure out Objective-C with your current haphazard approach. The code you posted is evidence of this.

You should be able to figure it out by following an online tutorial, or maybe even just by reading the Objective-C reference docs on Apple's website.

That's only half the battle, though. The other half is understanding the Cocoa class library, both Foundation and AppKit or UIKit. If you have solid grounding in Objective-C, then again, reading only Apple's docs might work. Evidence is, however, that you lack a sufficient grounding.

PhoneyDeveloper
Aug 13, 2010, 11:55 AM
IMO, and in short, the fastest way to learn iPhone development, and most other things, is by directed learning. This means taking a class in-person, reading a book for beginners, or following an online course.

The student needs to build up a scaffolding of understanding of the material. Once this scaffolding has been built up then learning new things becomes easy, because a new thing is small relative to the size of the existing knowledge. The beginner has insufficient experience to know what is important and what isn't. The beginner needs to be told what to learn. Directed learning is the way.

Get a book for beginners, follow an online course, or take a class in-person. You will save yourself a lot of time and headaches by doing it that way.

Stay away from online tutorials and only use Google to find the best book or online course or in-person class.

blrogers
Aug 13, 2010, 12:47 PM
Thanks Robie,

For the link to some beginners stuff.
Exactly what I needed for some ground level knowledge on Objective-C