NSString object not initialized properly - objective-c

Can somebody tell me what I am doing wrong here. I am trying to initialize a string from a list of predefined names, but for some reason it doesn't work for NSString that is a class field:
MyController.h:
#interface MyController:UIViewController
{
NSString *text;
}
MyController.m:
-(void) viewDidLoad
{
[super viewDidLoad];
NSArray *list = [NSArray arrayWithObjects:#"A", #"B", #"C", nil];
NSString* temp= [list objectAtIndex:0]; // temp = "A"
text = [list objectAtIndex:0]; // Why text = nil ????
text = #"Hello"; // does not work either
}

Turns out the debugger is wrong in this case.

Try setting text equal to NSString *text = [NSString stringWithFormat:#"%#", [list objectAtIndex:0]];

Related

NSArray BAD ACCESS

I'm trying to do a pickerView but I'm getting bad acess:
here is my code
-(void) viewWillAppear:(BOOL)animated {
list = [[NSArray alloc]init];
[self populateList]
}
-(void) populateList {
NSString *path = [[NSBundle mainBundle] pathForResource:#"nameoffile" ofType:#"txt"];
NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
list = [file componentsSeparatedByString:#"\n"];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return (NSString *)[list objectAtIndex:row]; //here I'm getting bad acces
}
The error is: "Thread 1: EXC_BAD_ACCESS(code=1, address=0xa001cc65)"
NSArray returned by componentsSeparatedByString: is autoreleased value so you need to retain it.
You should remove:
list = [[NSArray alloc]init];
and add retain to:
list = [[file componentsSeparatedByString:#"\n"] retain];

Unknown recieiver 'indexPath'l did you mean NSIndexPath?

I'm parsing something right now and i want to display it in a cell i', using this code for my tableview but i also want it without Tablview
NSString *const JsonDataUrl = [NSString stringWithFormat: #"%#", appdownloadurl];
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:JsonDataUrl]
encoding:NSUTF8StringEncoding
error:nil];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
parser = nil;
[self setTableData:[results objectForKey:#"resultCount"]];
This works great but when i place this line in viewdidload
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
NSLog(#"Apple: %#", [item objectForKey:#"price"]);
And error shows up on this line;
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
//Unknown recieiver 'indexPath'l did you mean NSIndexPath?
does anyone know how to fix this?
Do you declare any variable called indexPath in your viewDidLoad method? You can't just use the indexPath from your table view methods in viewDidLoad arbitrarily.
NSDictionary *item = [tableData objectAtIndex:[indexPath row]]; suggests you are trying to load from an NSArray, you probably want to try something like:
NSDictionary *item = [tableData objectForKey:#"Some Key"];

UITableView Cell Dynamic Array not Loading

Please help me figure out why the first line does not print the cell text while the second one does:
cell.textLabel.text = [NSString stringWithFormat:#"Hello %#", [swaItems objectAtIndex:indexPath.row]];
cell.textLabel.text = #"Hello";
No error is generated at any time.
Where you want the cell to initialize :
NSArray *swaItems = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
NSString *string = [swaItems objectAtIndex:indexPath];
NSLog(#"%#", string);
cell.textLabel.text = string;
Works for me.

Objective C: combine two elements of a vector

I have this code:
cell.textLabel.text = [[appDelegate.client objectAtIndex:indexPath.row] objectAtIndex:0];
but I want write in the cell table view also the "objectAtIndex:1" then after
...objectAtIndex:0...????
what can I write?
NSString *a = [[appDelegate.client objectAtIndex:indexPath.row] objectAtIndex:0];
NSString *b = [[appDelegate.client objectAtIndex:indexPath.row] objectAtIndex:1];
cell.textLabel.text = [NSString stringWithFormat:#"%#%#", a, b];
See NSString Class Reference and Formatting String Objects.

NSArray writeToFile returning "is deprecated" - iPhone SDK

I'm trying to save some settings from my app to a plist file when it closes, then load them when the app launches. I have 4 numbers saved in an NSArray called array. The loading works fine because if I change the file, the app starts the way the file was changed.
This code works fine:
- (void)LoadSettings {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:kSaveFileLocation];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:finalPath];
clockFaceImageSlider.value = [[array objectAtIndex:0] integerValue];
HourTypeSwitch.on = [[array objectAtIndex:1] integerValue];
touchRotationSwitch.on = [[array objectAtIndex:2] integerValue];
accelerometerRotationSwitch.on = [[array objectAtIndex:3] integerValue];
}
This code works up to the save line:
- (void)SaveSettings {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:kSaveFileLocation];
NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:clockFaceImageSlider.value], [NSNumber numberWithInt:HourTypeSwitch.on], [NSNumber numberWithInt:touchRotationSwitch.on], [NSNumber numberWithInt:accelerometerRotationSwitch.on], nil];
if (![array writeToFile:finalPath atomically:YES]) {
NSLog(#"error");
}
//The log does print error
}
Does anybody know how I can make it save to the plist?
~thanks
Dont write array's description NSString, just write the array instead:
[array writeToFile:finalPath atomically:YES];
Or if you want the strings you should use en encoding type
NSError *error;
[[array description] writeToFile:finalPath atomically:YES encoding:NSUTF8StringEncodingerror:&error];
but this will not write a plist.
Instead of taking the description (which is an NSString) of an NSArray and write it to a file, just use writeToFile:atomically of NSArray itself, as in
[array writeToFile:finalPath atomically:YES];
The reason writeToFile:atomically: of an NSString is deprecated is that it doesn't correctly account for the character encodings. Who told you to use description? That person/book should be punished...
That said, if you want to save just a few entries, it would be easier to just use NSUserDefaults, see here. Then all you have to do is
[[NSUserDefaults standardUserDefaults] setObject: ... forKey:#"..."]
and the framework does everything from preparing a plist path behind the scenes to saving it to the disk.