I am trying to use NsKeyedArchiever to take save Car details which contains model,make,year,color,vin.
The code for this is as follows :
void CarData()
{
Car *myCar = [[Car alloc] init];
myCar.model = [carTempArray objectAtIndex:0];//Temporary array contains value
myCar.make = [carTempArray objectAtIndex:1];
myCar.color = [carTempArray objectAtIndex:2];
myCar.year = [carTempArray objectAtIndex:3];
myCar.vin = [NSNumber numberWithInt:check];
NSString *docspath = [NSHomeDirectory() stringByAppendingPathComponent:#"documents"];
BOOL saved =[NSKeyedArchiver archiveRootObject: myCar toFile:docspath];
NSLog(#"did save state %d",saved);
[myCar release];
[NSKeyedArchiver release];
}
And after that using NSkeyedUnarcheiver as follows :
void ShowData()
{
NSString *docspath = [NSHomeDirectory() stringByAppendingPathComponent:#"documents"];
Car * retrieveCar = [NSKeyedUnarchiver unarchiveObjectWithFile:docspath];
// for(int i=0 ; i<studentList.count;i++)
{
// Car * retrieveCar =[studentList objectAtIndex:0];
NSString * first = retrieveCar.model;
NSString * first1 = retrieveCar.make;
NSString * first2 = retrieveCar.color;
NSString * first3 = retrieveCar.year;
NSLog(#"%#",first );
NSLog(#"%#",first1 );
NSLog(#"%#",first2 );
NSLog(#"%#",first3 );
}
But this only takes one value and uses one value.I want to save the NSKeyedArchiever object in an array so that multiple cars can be a part of it using same code.And Load the same as above.Please rectify the problem .I want to run the code for multiple cars and i am able to perform the following tasks in array of cars as searching a car,deleting a car record and adding a car.
Related
I could need some help.
I'm using XLForm in my Objective-C project. I have a first row with a picker where you have four different options. Then there is a second picker. But the second picker should only be available if a certain value is selected in the first picker.
I know there is a description on the github-site but I don't really understand what to do. So please help me doing this.
Here are the relevant parts of my code:
row = [XLFormRowDescriptor formRowDescriptorWithTag:#"repeat" rowType:XLFormRowDescriptorTypeSelectorPickerViewInline title:#"Wiederholen:" ];
row.cellClass = [LehrerXLFormInlineSelectorCell class];
NSMutableArray *selectionArray = [NSMutableArray array];
for (NSNumber *item in [APIDataReplacement appointmentRepeatingList]) {
NSString *title = [APIDataReplacement appointmentRepeatingToString:item.intValue];
[selectionArray addObject:[XLFormOptionsObject formOptionsObjectWithValue:item displayText:title]];
}
row.selectorOptions = selectionArray;
if ([selectionArray count] == 0) {
[row setDisabled:#YES];
}
if (aItem) {
int repeatingID = [[aItem appointmentRepeatingId] intValue];
NSString *title = [APIDataReplacement appointmentRepeatingToString:repeatingID];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:[NSNumber numberWithInt:repeatingID] displayText:title];
}else{
NSNumber *first = [[APIDataReplacement appointmentRepeatingList] firstObject];
NSString *title = [APIDataReplacement appointmentRepeatingToString:first.intValue];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:first displayText:title];
}
[section addFormRow:row];
section = [XLFormSectionDescriptor formSectionWithTitle:#""];
[form addFormSection:section];
row = [XLFormRowDescriptor formRowDescriptorWithTag:#"weekday" rowType:XLFormRowDescriptorTypeSelectorPickerViewInline title: #"Wochentag:"];
row.cellClass = [LehrerXLFormInlineSelectorCell class];
NSMutableArray *selectionArray1 = [NSMutableArray array];
for (NSNumber *item in [APIDataReplacement dayList]) {
NSString *title = [APIDataReplacement dayToString:item.intValue];
[selectionArray1 addObject:[XLFormOptionsObject formOptionsObjectWithValue:item displayText:title]];
}
row.selectorOptions = selectionArray1;
if (aItem) {
// AppointmentRepeating *currentRepeat = [aItem appointmentRepeating];
int dayID = [[aItem startday] intValue];
NSString *title = [APIDataReplacement dayToString:dayID];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:[NSNumber numberWithInt:dayID] displayText:title];
}else{
NSNumber *first = [[APIDataReplacement dayList] firstObject];
NSString *title = [APIDataReplacement dayToString:first.intValue];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:first displayText:title];
}
[section addFormRow:row];
section = [XLFormSectionDescriptor formSectionWithTitle:#""];
[form addFormSection:section];
These are the two pickers. I think I should do something in the formRowDescriptorValueHasChanged-method, but I don't know what. If the first picker has the value "20" then the second picker should be hidden.
Really appreciate your help.
For your second XLFormRowDescriptor you should:
secondRow.hidden = [NSString stringWithFormat:#"$%# == 20", firstRow];
firstRow is the first XLFormRowDescriptor.
This actually creates an NSPredicate that is automatically evaluated whenever values change and sets the visibility appropriately.
Taken from XLForm's example:
For example, you could set the following string to a row (second) to
make it disappear when a previous row (first) contains the value
"hide".
second.hidden = [NSString stringWithFormat:#"$%d contains[c] 'hide'", [first.value intValue]];
Edit: I changed the predicate to work with NSNumber values.
I have an iPad that reads data from an SQL database. The following code works fine and retrieves 2 fields from each record and reads them into an NSArray.
I now need to read 5 of the fields and I can't help but think that there is a better way of doing it rather than running 5 separate queries through php (the getinfo.php file with the choice parameter set to pick the different fields).
Any pointers to a better method for doing this?
NSString *strURLClass = [NSString stringWithFormat:#"%#%#", #"http://wwwaddress/getinfo.php?choice=1&schoolname=",obsSchoolName];
NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsClass = [observationsArrayClass objectEnumerator];
NSString *strURLDate = [NSString stringWithFormat:#"%#%#", #"http://wwwaddress/getinfo.php?choice=5&schoolname=",obsSchoolName];
NSArray *observationsArrayDate = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLDate]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsDate = [observationsArrayDate objectEnumerator];
id className, dateOfObs;
while (className = [enumForObsClass nextObject])
{
dateOfObs = [enumForObsDate nextObject];
[observationListFromSQL addObject:[NSDictionary dictionaryWithObjectsAndKeys:className, #"obsClass", dateOfObs, #"obsDate",nil]];
}
Yes, you can do that with less code by "folding" the statements into a loop, and using a mutable dictionary:
// Add other items that you wish to retrieve to the two arrays below:
NSArray *keys = #[#"obsClass", #"obsDate"]; // Key in the dictionary
NSArray *choices = #[#1, #5]; // Choice in the URL string
NSMutableArray *res = [NSMutableArray array];
NSMutableArray *observationListFromSQL = [NSMutableArray array];
for (int i = 0 ; i != keys.count ; i++) {
NSNumber *choice = choices[i];
NSString *strURLClass = [NSString stringWithFormat:#"http://wwwaddress/getinfo.php?choice=%#&schoolname=%#", choice, obsSchoolName];
NSArray *observationsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
NSEnumerator *objEnum = [observationsArrayClass objectEnumerator];
NSString *key = keys[i];
NSMutableDictionary *dict;
if (res.count < i) {
dict = res[i];
} else {
dict = [NSMutableDictionary dictionary];
[res addObject:dict];
}
id item;
while (item = [objEnum nextObject]) {
[res setObject:item forKey:key];
}
}
I want store 4 name of book from url (.php?info=1&b=1 display name of first book) in array and run that code and use these names.
also I want create object of class and dedicate first name to first object (second name to second object and etc)
this is my code:
#import "Recipe.h"
for (int i = 1; i <= 4; i++)
{
Recipe *booki = [Recipe new]; // create object
NSLog(#"%d,%#",i,booki);
NSString *c = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.102:81/temp/book.php?info=1&b=%d",i]]];
NSMutableArray *b = [[NSMutableArray alloc]init];
[b addObject:c];
NSLog(#"%#,%#",c,b);
booki.name = [NSString stringWithString:c];
recipes = [NSMutableArray arrayWithCapacity:4];
//NSLog(#"%#",recipes);
recipes = [NSMutableArray arrayWithObjects:booki, nil];
in this code I see only one name in object and its last name of book
The issue seems to be this.
recipes = [NSMutableArray arrayWithObjects:si, nil];
Since you are looping inside a for loop. If you want to have all names. You need to create an array outside the for loop. Instead of creating a new array each time add object to it.
if(!recipes){
recipes = [NSMutableArray array];
}
[recipes addObject:si];
I've got this response from JSOn web service:
categoria = (
{
"id_categoria" = 61;
imagen = "http://exular.es/mcommerce/image/data/ipod_classic_4.jpg";
nombre = Gorras;
},
{
"id_categoria" = 59;
imagen = "http://exular.es/mcommerce/image/data/ipod_touch_5.jpg";
nombre = Camisetas;
},
{
"id_categoria" = 60;
imagen = "http://exular.es/mcommerce/image/data/ipod_nano_1.jpg";
nombre = Pantalones;
}
);
}
It is feed in a dictionary in this piece of code:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//data =nil;
NSLog(#"JSNON %#", responseString);
NSDictionary *results = [responseString JSONValue];
NSLog(#"JSNON %#", results);
I need to iterate all the categories in the dictionary and create an NSArray that contains objects Categories with properties "name" and image".
Many thanks
NSArray *categories = [results objectForKey:#"categoria"];
This will create an array of your category objects. You can pass this array of dictionaries/objects to your custom object or just iterate through it pulling what you need.
Category *cat = [[Category alloc] initWithDictionary:[categories objectAtIndex:0]];
This is just assuming your init is set to handle a dictionary. There isn't enough code to know, but you should be able to figure it out from here.
I am trying to generate an NSDictonary that can be used to populate a listview with data I retrieved from an SQL statement. when I go to create an array and add them it adds the arrays for ALL my keys and not just for the current key. I've tried a removeAllObjects on the array but for some reason that destroys ALL my data that I already put in the dictionary.
//open the database
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select alphaID, word from words order by word";
sqlite3_stmt *selectStatement;
//prepare the select statement
int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
if(returnValue == SQLITE_OK)
{
NSMutableArray *NameArray = [[NSMutableArray alloc] init];
NSString *alphaTemp = [[NSString alloc] init];
//loop all the rows returned by the query.
while(sqlite3_step(selectStatement) == SQLITE_ROW)
{
NSString *currentAlpha = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 1)];
NSString *definitionName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2)];
if (alphaTemp == nil){
alphaTemp = currentAlpha;
}
if ([alphaTemp isEqualToString:(NSString *)currentAlpha]) {
[NameArray addObject:definitionName];
}
else if (alphaTemp != (NSString *)currentAlpha) {
[self.words setObject:NameArray forKey:currentAlpha];
[NameArray removeAllObjects];
[NameArray addObject:definitionName];
}
}
}
The Statement above adds all the "keys" but then removes all the array elements for all keys. if I take out the removeAllKeys it adds ALL the array elements for ALL keys. I don't want this I want it to add the array elements FOR the specific key then move on to the next key.
in the end I want a NSDictonary with
A (array)
Alpha (string)
Apple (string)
B (array)
Beta (string)
Ball (string)
C (array)
Code (string)
...
Though I don't think it affects your problem, from the way I read your code, you should change
NSString *alphaTemp = [[NSString alloc] init];
to
NSString *alphaTemp = nil;
since alphaTemp is just used to point to an NSString that is generated initially as currentAlpha. You also should call [NameArray release] at some point below the code you've given, since you alloc'd it.
The real issue is that you are repeatedly adding pointers to the same NSMutableArray to your NSDictionary (self.words). I can see two ways to fix this:
Change
[self.words setObject:NameArray forKey:currentAlpha];
to
[self.words setObject:[NSArray arrayWithArray:NameArray] forKey:currentAlpha];
so that you are adding a newly-created (non-mutable) NSArray to your NSDictionary.
-- or --
Insert
[NameArray release];
NameArray = [[NSMutableArray alloc] init];
after
[self.words setObject:NameArray forKey:currentAlpha];
so that once you've inserted the NSMutableArray into the NSDictionary, you create a new NSMutableArray for the next pass.