|
|
#1 |
|
Saving to Plist file.
Hey
As I'm getting a bit better and understanding some things more and more. I'm in this situation where I want to add new strings to my tableview. So I've setted up everything, and made an addViewController with a textfield etc. When someone presses the button this will be the Action: Code:
-(IBAction)doneKnop:(id)sender{
NSString *myString = vraagVeld.text;
[arrayData addObject:myString];
[arrayData writeToFile:@"opties.plist" atomically:NO];
[[self navigationController] popViewControllerAnimated:YES];
}
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"opties" ofType:@"plist"];
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.arrayData = data;
}
Grz Last edited by Mvkoe; Jan 7, 2013 at 11:48 AM. |
|
|
|
0
|
|
|
#2 |
|
Where do you think you are saving the file to?
BTW, here are the documents from the apple site. Look up writeToFile and see what the requirements are for that method. The answer it there. https://developer.apple.com/library/...occ/cl/NSArray
__________________
I know more than yesterday. Lars |
|
|
|
0
|
|
|
#3 | |
|
Quote:
Code:
-(IBAction)doneKnop:(id)sender{
NSString *myString = vraagVeld.text;
[arrayData addObject:myString];
NSString *path = [[NSBundle mainBundle] pathForResource:@"opties" ofType:@"plist"];
[arrayData writeToFile:path atomically:YES];
NSLog(@"%@", arrayData);
[[self navigationController] popViewControllerAnimated:YES];
}
Code:
(
"Wie betaald?",
"Wie is bob?",
"Wie is Wat?",
"Wie ben ik?",
blah
)
|
||
|
|
0
|
|
|
#4 |
|
What exactly is happening (or not happening) that makes you feel your file is not being written?
The more details you give about what you've tried, the better the help you will receive.
__________________
13" MacBook Pro, 2.26 GHz, 4 GB RAM, 500 GB HD iPhone 4 32GB 2nd gen iPod nano 4GB
|
|
|
|
0
|
|
|
#5 |
|
Your application bundle is read-only. On the device you can't write a file there. You should decide on a location for your plist file that is in your Application Support folder. When your app launches check if the file exists in the Application support folder. If it isn't there then copy the version from your bundle to the Application Support folder. After that always read/write the file from the Application Support folder.
BTW, writeToFile: returns a value telling you if it worked. You should inspect that value. |
|
|
|
0
|
|
|
#6 | |
|
Quote:
So what I need to do is.
Am I right? EDIT: Okay, so it's working now, i can read and write to it. But now the problem is, that my TableView doesn't update. When i press the done button: Code:
[[self navigationController] popViewControllerAnimated:YES]; Last edited by Mvkoe; Jan 4, 2013 at 04:07 AM. |
||
|
|
0
|
|
|
#7 |
|
The usual way to update a table view with new data is to 1, update your data model, 2, [self.tableView reloadData].
|
|
|
|
0
|
|
|
#8 |
|
Thanks for the help, I already figured it out by searching a bit, and remaking the adding, because the self.tableview can't be used in my adding vc.
|
|
|
|
0
|
|
|
#9 |
|
All the more reason to be using a model layer. A viewController updates the model data and then another viewController retrieves that model data.
__________________
|
|
|
|
0
|
|
|
#10 |
|
|
0
|
|
|
#11 |
|
Have you finished reading "Beginning iOS 5 Programming" yet? That's the David Mark / Jack Nutting / Jeff LaMarche one you have, yes?
P.S. Be sure to check out this Apple doc as well: Concepts in Objective-C Programming
__________________
|
|
|
|
0
|
|
|
#12 | |
|
Quote:
|
||
|
|
0
|
|
|
#13 |
|
Slow down, don't get ahead of yourself, step away from the real coding and finish reading the book. Make sure you're comfortable with everything it's covered. If not, read it again. Take working apps and tweak things. Read through that Apple doc I linked to. Figure out what areas you need to dig deeper in to understand. Take it easy.
__________________
|
|
|
|
0
|
|
|
#14 | |
|
Quote:
So the Model Layer, is an NSobject class, that i need to alloc and init in each class i want to use. Then with getters and setter using the class ? Like this?: http://developer.apple.com/library/i...011318-CH9-SW1 EDIT: Okay so it's going quite well! I'm almost getting there, so i have made a model layer with all my data things in. I can get it in my tableview etc, but now i got one problem. When adding something new to my tableview it gives an error that i don't understand: Code:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' Code:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
NSString *naarArray = [actionSheet textFieldAtIndex:0].text;
[self.dataController.vraagArray addObject:naarArray];
[self.dataController saveData];
[self.tableView reloadData];
}
}
Last edited by Mvkoe; Jan 5, 2013 at 05:42 AM. |
||
|
|
0
|
|
|
#15 |
|
That error says that somewhere in your code there is a call to insertObject:AtIndex: on an NSArray. That method can only be called on an NSMutableArray.
Look up the words mutable and immutable and understand how they apply to all the container classes in Foundation Kit. You need to verify the place in your code that this error is occurring. One way to do that is to set a breakpoint on All Exceptions and then run the app in the debugger until you get the error. At that point the app will be stopped in the debugger and you should see the stack trace in the debugger stack trace view. Another way is to run the app outside the debugger on the device and get a crash log. |
|
|
|
0
|
|
|
#16 | |
|
Quote:
I'll try setting a breakpoint, to bad i don't have expierence with breakpoints.. EDIT: Okay so i've setted a breakpoint, did it step by step, seems all fine by me, putting an NSString in my @property (nonatomic, copy) NSMutableArray *data; And then suddenly it jumps to this code: Code:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
->>>>>> return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Hmm okay, by changing my @property (nonatomic, copy) to @property (nonatomic, strong) it works.. But it was told to me that you should always use copy as best option for Strings,arrays etc.. How come i need to use Strong in this case ? Can someone explain ? Last edited by Mvkoe; Jan 5, 2013 at 12:42 PM. |
||
|
|
0
|
|
|
#17 |
|
Hoist by his own petard.
What do you think this does? Code:
_myMutableArray = [[[NSMutableArray alloc] init] copy]; When you're stopped at a breakpoint move the slider at the bottom of the pane all the way to the right so you can see the full stack trace. |
|
|
|
0
|
|
|
#18 |
|
Mvkoe - If you are on Chapter 10 of that book you should have a good idea of what mutable and immutable are like NSArray and NSMutableArray. I would slow down and even start over in the book and really get it. dejo mentioned that you should understand what you are reading before you move on, and it is a good idea.
When I was learning I did the same thing you are doing now and reading your posts sounds familiar. If you treat programming like a race you will lose. It sounds like you are reading every chapter and before you understand the material you are moving on and skipping over what you don't understand. Your probably copying and pasting lots of code from the internet to Frankenstein your program together. If you are truly coming from a Java background then the cross over should not be that hard. Java deals with mutable and immutable object too. I would go back to square one and really understand every chapter by writing lots of small programs that cover the material in that chapter. That is what was recommended to me when I started but I thought that knew better and soon I was asking for help like you are now because I did not get it. My 2 cents if you want to be a good programmer, which I hope to be some day too.
__________________
I know more than yesterday. Lars |
|
|
|
1
|
|
|
#19 | ||
|
Quote:
The only problem is that I need to get deeper in immutable and mutable things, and trying to understand the thing of "copy". In my code i'm always trying to improve things so that it's clean. Quote:
|
|||
|
|
0
|
|
|
#20 | |
|
Quote:
Wat je doet is: Je roept een nieuw object tot leven van type NSMutableArray. Daarna stuur je init naar dat object. Niets aan de hand. Maar wat je aan _myMutableArray geeft is een KOPIE van dat object. Dus opeens bestaan er 2! Een waar _myMutableArray naar wijst en 1 waar eeeh... niets meer naar wijst. Dat is een memory leak. Als je 'strong' gebruikt is dat ongeveer gelijk aan het oude 'retain' in Obj-C. Je neemt eigenlijk het eigenaar zijn op je. Wat er dan gebeurt is: Maak een NSMutableArray initialiseer, en RETAIN. Dat betekent dat de ontvangende parameter (_myMutableArray) voor een gedeelte eigenaar wordt van dat object. Er is geen kopie meer, er is alleen bekend dat er MINSTENS 1 echte eigenaar van is. Zodra je die 'copy' doet is er alleen een eigenaar van het gekopieerde object. Aangezien er geen eigenaar was van het origineel blijft deze 'rondzweven' in het geheugen. |
||
|
|
0
|
|
|
#21 | |
|
Quote:
-- I'll just start reading some basic Objective-C books, for getting really then hang of these things! |
||
|
|
0
|
|
|
#22 |
|
Never had anyone go Dutch on me before.
This issue is a somewhat subtle issue that I wouldn't expect a beginner to figure out. The rules for when to make a @property copy or strong are a little complicated. The simple rule is "Never mark a @property as "copy" for a mutable type." If you change your @property for the NSMutableArray to be "strong" and not "copy" things should work the way you expect. The copy method returns an immutable object. mutableCopy returns a mutable object but there is no "mutable copy" adornment for @properties. Using "copy" protects your code against setting a mutable object when you want an immutable object. But there is no need to have this kind of protection when your object type is mutable. |
|
|
|
0
|
|
|
#23 |
|
Thanks for all the help people! I'm getting there, made a singleton. I can code the things I want without copy pasting code from the internet!
I'm pretty happy for now!
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 05:54 PM.







13" MacBook Pro, 2.26 GHz, 4 GB RAM, 500 GB HD
I support the
Linear Mode
