I have read a few posts and found a method. I adapted it to my app, however at [self saveContext]; I get a bad instruction error.
Please let me know why, and if this method makes sense. Also I am using \r because in the csv each item is in its own row.
Thank you in advance
-(void)addData{
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:#"coredatainfo.csv"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:#"\r"];
[dataFile release];
FirstCDitem *myItem;
for (int i = 0 ; i < [dataRows count] ; i++)
{
NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:#","];
NSLog(#"Added: %d %#",i,dataElements);
myItem = (FirstCDitem *)[NSEntityDescription insertNewObjectForEntityForName:#"FirstCDitem" inManagedObjectContext:[self managedObjectContext]];
[FirstCDitem setTitle:[dataElements objectAtIndex:i]];
[self saveContext];
}
}
There are two problems in this code line:
[FirstCDitem setTitle:[dataElements objectAtIndex:i]];
i is the current row number, so it probably makes no sense to use it as index to the dataElements array. Perhaps you want the first element in the row?
setTitle must sent to the instance myItem, not to the class FirstCDItem.
So the line should look like this:
[myItem setTitle:[dataElements objectAtIndex:0]];
Related
I am trying to write the array data into excel (actually it is a CSV, but it is opened in excel). I used the following code to do that:
NSMutableArray *list;
list = [[NSMutableArray alloc] init];
NSString *string = [list componentsJoinedByString:#","];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"yourFileName.csv"];
[data writeToFile:appFile atomically:YES];
It works fine, but the problem is I have 20 objects in my 'list' array and all those 20 objects are written in side by side cells. What I want is to write the first 4 objects in one line and then move to the new line and again write the next 4 objects in that line till all the objects in the list array are completed.
Can anyone help me with this issue?
NSMutableArray *list = ...
NSMutableString *buffer = [NSMutableString string];
for (NSUInteger i = 0; i < list.count; i++) {
NSString *value = list[i];
if (i > 0) {
if (i % 4 == 0) { //after every 4th value
buffer.append("\n"); //end line
}
else {
buffer.append(",");
}
}
buffer.append(value);
//if your values contain spaces, you should add quotes around values...
//buffer.appendFormat(#"\"%#\"", value);
}
NSData *data = [buffer dataUsingEncoding:NSUTF8StringEncoding];
...
To break lines in CSV just input a \r\n in the "end of the line". Be caerfull because in the mac you only need \r or \n (not really sure right now)
I'm fairly new to Objective-C programming. I'm trying to build an app for personal use to view event info which is contained on my web server. I wish for each Event Title string to be shown as a separate row within my UITableView.
I'm using CFPropertyList to create the plist from MySQL. Here's what my plist looks like:
I'm downloading the plist like so:
NSString *stringURL = #"http://www.example.com/events.plist";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"events.plist"];
[urlData writeToFile:filePath atomically:YES];
}
Then this is how I'm trying to load the info into their arrays:
events = [[NSDictionary alloc] initWithContentsOfFile:filePath];
ID = [events objectForKey:#"Event ID"];
title = [events objectForKey:#"Event Title"];
date = [events objectForKey:#"Event Date"];
price = [events objectForKey:#"Price"];
totalTickets = [events objectForKey:#"Total Tickets"];
ticketsSold = [events objectForKey:#"Tickets Sold"];
ticketsRemaining = [events objectForKey:#"Tickets Remaining"];
postStatus = [events objectForKey:#"Post Status"];
postContent = [events objectForKey:#"Post Content"];
Now here's where I'm having trouble. Now when I'm debugging the number of rows the 'events' dictionary has it returns 0.
Therefore I cannot set the 'numberOfRowsInSection'. Nor does [title objectAtIndex:indexPath.row] work.
I believe the issue lays with how I'm setting up the NSDictionary, but due to my lack of Objective-C and array knowledge, I'm unable to overcome this issue.
Here's your problem:
events = [[NSDictionary alloc] initWithContentsOfFile:filePath];
Your plist is an array of dictionaries and you're trying to load that array as a dictionary. Try this instead:
events = [NSArray arrayWithContentsOfFile:filePath];
Edit: To extract the titles into an array so you can use them in table cells, do this:
NSMutableArray *titles = [NSMutableArray array];
for (NSDictionary *event in events) {
NSString *title = [event objectForKey:#"Event Title"];
[titles addObject:title];
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to populate UITableView with plist
I save data to plist like this:
- (IBAction)save:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"data.plist"];
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == data) {
data = [[NSMutableArray alloc] initWithCapacity:0];
}
else {
[data retain];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:label1.text forKey:#"Test1"];
[array setObject:label2.text forKey:#"Test2"];
[array setObject:label3.text forKey:#"Test3"];
[array setObject:label4.text forKey:#"Test4"];
NSLog(#"%#", array);
[data addObject:array];
[array release];
[data writeToFile:plistPath atomically: TRUE];
[data release];
}
NSLog:
2012-07-01 18:52:19.566 testapp[22651:707] {
Test1 = 40;
Test2 = 102;
Test3 = 153;
Test4 = 255;
}
How can I load saved data to uitableview?
To read it, do this:
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
You should then be able to read the values for all the keys like this:
NSString *myValue = [plistDict objectForKey:#"someKey"];
edit:
After rereading your code it seems like you might be doing something you don't want to do. Your current plist structure is a top level array that adds a dictionary to itself every time you invoke "save". Is this what you're trying to do? If so, what represents a cell in your table, an index in the array?
If so, then you want to do
// top level array
NSMutableDictionary* plistArray = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
// individual dictionary at one array index (such as indexPath.row in cellForRowAtIndexPath)
NSMutableDictionary *dictionary = [plistArray objectAtIndex:yourIndex];
In any case, the object you have named "array" is actually a dictionary so you should rename it (or change it to be an array if that's what you want).
After cleaning up my code and making it ready for deployment I encountered a weird problem. When I try to add a contact to my tableview it always crashes when the empty is array. After that its not problem to add as many as you want. And also when I delete a contact and the array is empty, it also crashes with the same error message:
Terminating app due to uncaught exception 'NSRangeException', reason:
' -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Here is what I do: I add a contact via php to my database, read it out with xml and give it back to the app so the user can instantly see what happened (adding a contact that is). There is a lot of code involved so I stick to the basics for now.
When the view loads the array gets allocated. User can add a contact. While adding the contact a php file is called that creates the xml file where the contacts are saved.
Here is where I get the object back and paste it into my array and display it
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.tabelle count]];
self.tableView.scrollEnabled = YES;
numbers = [[NSMutableArray alloc] init];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *fileName = [prefs stringForKey:#"number"];
NSString *cc = [prefs stringForKey:#"Code"];
NSString *urlString = [NSString stringWithFormat: #"http://myserver/%#%#.xml", cc, fileName];
NSURL *url = [NSURL URLWithString: urlString];
DataFileToObjectParser *myParser = [[DataFileToObjectParser alloc] parseXMLAtUrl:url toObject:#"contacts" parseError:nil];
for(int i = 0; i < [[myParser items] count]; i++) {
contacts *new = [[Telefonbuch alloc] init];
new = (contacts *) [[myParser items] objectAtIndex:i];
[numbers addObject:new];
[self.tableView reloadData];
And here is how it gets displayed, nothing special:
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[numbers objectAtIndex:indexPath.row] fname]];
NSString *TableText2 = [[NSString alloc] initWithFormat:#"%#", [[numbers objectAtIndex:indexPath.row] lname]];
NSString *cellValue = [NSString stringWithFormat:#"%# %#", TableText, TableText2];
cell.textLabel.text = cellValue;
Your loop sounds strange.
for(int i = 0; i < [[myParser items] count]; i++) {
contacts *new = [[Telefonbuch alloc] init];
new = (contacts *) [[myParser items] objectAtIndex:i];
[numbers addObject:new];
[self.tableView reloadData];
First avoid to use new keyword (as user1118321 already suggested).
Then, what is contacts? Is contacts a superclass of type Telefonbuch?
What do these two lines mean?
contacts *new = [[Telefonbuch alloc] init];
new = (contacts *) [[myParser items] objectAtIndex:i];
You alloc-init an instance of Telefonbuch class to a new (avoid this) variable but then you assign that variable to another object. Why?
The right code could be like the following.
for(int i = 0; i < [[myParser items] count]; i++) {
Telefonbuch* newContact = (Telefonbuch*) [[myParser items] objectAtIndex:i]; // or contacts* newContact = (contacts*) [[myParser items] objectAtIndex:i];
// maybe it could be better to rename contacts with a capital letter
[numbers addObject:newContact];
}
[self.tableView reloadData];
Some notes
If you want to add or delete item to a table view, you need to do it in 2 stages:
1) Deal with your model
[yourModel removeObjectAtIndex:row];
2) Deal with your table’s animation
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
If you need to download content, maybe you could consider to do it asynchrously without blocking the main thread. In particular, this is could be the synchronous call that could be a blocking one (since I don't have any details I'm only supposing it).
DataFileToObjectParser *myParser = [[DataFileToObjectParser alloc] parseXMLAtUrl:url toObject:#"contacts" parseError:nil];
If you don't use ARC, pay attention to memory management.
Hope it helps.
i have the following code and i want to use NSMutable arrays instead of NSArray
could you tell me how to load the NSMutable array, as the current method does not work.
-(void) whatever{
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: #"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *titles = [xpathParser search:#"//h3"]; // get the page title - this is xpath notation
TFHppleElement *title = [titles objectAtIndex:0];
NSString *myTitles = [title content];
NSArray *articles = [xpathParser search:#"//h4"]; // get the page article - this is xpath notation
TFHppleElement *article = [articles objectAtIndex:0];
NSString *myArtical = [article content];
i have tried :
NSMutableArray *titles = [xpathParser search:#"//h3"];
but it does load the values?
You can invoke mutableCopy on an NSArray object to return to you an NSMutableArray.
Note that the callee will obtain ownership of this object since the method name contains "copy".
(Apple's memory management guide states that a method name containing the words "alloc", "new" or "copy" should by convention return an object which you own, and as such must relinquish ownership of at some point.)
Simply like this:
NSArray* someArray = [xpathParser search:#"//h3"];
NSMutableArray* mutableArray = [someArray mutableCopy];
That's quite literally, it.
Assuming [xpathparser ...] returns an NSArray, you can use:
NSMutableArray *titles = [NSMutableArray arrayWithArray:[xpathParser search:#"//h3"]];