Hi I have created an array from core-data using:
NSArray* invoiceItem =[fetchedResultsController fetchedObjects];
which returns the following according to the log:
"<Invoice: 0x8545900> (entity: Invoice; id: 0x8542dd0 <x-coredata://AF2BBB5C-4135-45EB-A421-5036AE02D2A0/Invoice/p19> ;
data: {\n GSTAmount = \"0.75\";\n amountPaid = nil;\n cardID = 0;\n
customer = \"\";\n date = \"23/12/2011\";\n incTaxPrice = \"8.25\";\n
incTaxTotal = \"8.25\";\n invoiceNumber = a20;\n itemCode = 1035;\n
paymentMethod = nil;\n price = \"7.50\";\n quantity = 1;\n saleStatus = I;\n
taxCode = GST;\n timeStamp = \"2011-12-22 22:10:25 +0000\";\n total = \"7.5\";\n})",
"<Invoice: 0x8545c00> (entity: Invoice; id: 0x8543390 <x-coredata://AF2BBB5C-4135-45EB-A421-5036AE02D2A0/Invoice/p20> ;
data: {\n GSTAmount = \"0.55\";\n amountPaid = nil;\n cardID = 0;\n
customer = \"\";\n date = \"23/12/2011\";\n incTaxPrice = \"6.05\";\n
incTaxTotal = \"12.1\";\n invoiceNumber = a20;\n itemCode = 1040;\n
paymentMethod = nil;\n price = \"5.50\";\n quantity = 2;\n saleStatus = I;\n
taxCode = GST;\n timeStamp = \"2011-12-22 22:11:14 +0000\";\n total = 11;\n})"
)
My overall aim is to simply create an string of the itemCode property formatted so that it can be a column in a pdf as I do not know any other way of making a table other than creating an image of the tableview and inserting that in the PDF. I want to avoid doing that.
So instead I am trying to get a string from the above array, formatted as below
"1035\n1040"
I don’t know how to get the item code property from the array by itself. Note the Item code will vary in length and will not always be just numbers.
Any help will be appreciated!!! If anyone has any other tips or a better way to achieve what I am trying to do, i’m all ears 🙂
Edit
Purely for breadth of solutions. I happened to come up with a solution after I stepped away from the computer for a couple minutes. I’m not going with my solution as the other seems to be a bit more efficient/to the point. However I thought I would put it up for others to see another method.
NSMutableArray *itemCodes =[invoiceItem mutableArrayValueForKey:@"itemCode"];
NSString *holdingString =[NSString stringWithFormat:@"%@",itemCodes];
NSString *itemColumn = [holdingString stringByReplacingOccurrencesOfString:@"," withString:@"\n"];
First get an array of the values you want. Then join those values with a string. In your case a newline character.
You could iterate over the properties of the entity (NSEntityDescription) property of your Invoice managed object and create a dictionary of properly formatted strings keyed by each property’s name.
If I may suggest a minor change that could make your code more readable. Instead of
You might consider renaming the variable to invoiceItems or results to help identify the variable as a “container” as opposed to an “instance”.