NSArray question - \n\t\t being added to the end of strings? - iphone

My strings are fine, but when they get added to an array a weird grouping of "\n\t\t" gets added to the end. So the string "Apple", becomes "Apple\n\t\t" when I look at the array description.
Whats the deal with that? and how do I fix it?
Addition:
Its XML code. It seemed like [currentPlace appendString:string]; would get the right part, then add the \n\t\t next time round. I solved this by alternating using a boolean:
if (alternate == NO) {
[currentPlace appendString:string];
alternate = YES;
} else {
alternate = NO;
}
Is there a better way to alternate? I remember there being a case + break way, but I forget how to do it.

Adding strings to an array obviously does not modify their contents. Your bug is somewhere else, a part of your program you did not describe. Without code, nobody would be able to help you.

I solved this by adding a switch to:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
All is good now.

Related

How to add a simple function at top of FirstViewController.h

I'm writing a simple app with 10 buttons on it. One button looks like this:
- (IBAction)num1pressed:(UIButton *)sender
{
// checkSomethingFirst
self.myLabel.text=[NSString stringWithFormat:#"1"];
}
Every button needs to "check something first" so rather than copy and paste the lines of code into every (IBAction)num#pressed code segment, I want to call a function called checkSomethingFirst. Maybe function is the wrong word here? Nevertheless my function looks like this:
-(void)checkSomethingFirst
{
// check first thing
// check other thing
// check last thing
}
When I compile my code - which looks like this:
- (IBAction)num1pressed:(UIButton *)sender
{
checkSomethingFirst;
self.myLabel.text=[NSString stringWithFormat:#"1"];
}
I get an error at checkSomethingFirst; <--- Use of undeclared identifier 'checkSomethingFirst'.
How do I create a simple function or procedure that all of my buttons can use? Note I don't need to return any values as my checkSomethingFirst function simply sets a label to either a 0 or a 1 (probably better off using a bool, except I'm doing the setting on a global label - hence no need to return a bool, I can simply interrogate the value in the label).
You need to say what object the method should be called on
[self checkSomethingFirst];
You are calling your method in the wrong way
- (IBAction)num1pressed:(UIButton *)sender
{
[self checkSomethingFirst];
self.myLabel.text=[NSString stringWithFormat:#"1"];
}
You need to call that method using
[self checkSomethingFirst];
hope it helps.
- (IBAction)num1pressed:(UIButton *)sender
{
// checkSomethingFirst
[self checkSomethingFirst];
self.myLabel.text=[NSString stringWithFormat:#"1"];
}

Prevent Calculator Syntax Error Crash? Xcode

Currently I am making a Calculator that allows the user to type out the formula they wish.
Ex. ((1+1)**9)+2)
This works just fine, I have used two methods for calculating this.
First:
answer = [[NSExpression expressionWithFormat:typeTo.text, nil] expressionValueWithObject:nil context:nil];
typeTo.text = [NSString stringWithFormat:#"%#", answer];
answerLabel.text = [NSString stringWithFormat:#"ANS { %# }", answer];
Second:
answer = [GCMathParser evaluate:typeTo.text];
Both of these calculate the problem without difficulty. But if the user types in:
(1+1)) [two brackets]
Both ways crash. This is one example of many different syntax errors. Is there a way to easily prevent this?
.
Additional info:
This is the way the second method catches the error:
#ifdef __COCOA_IMPLEMENTATION__
[NSException raise:#"Error in expression" format:#"error = %s", errStr];
#endif
THANKS :D
I haven't used either of those but based on the additional info, it may be throwing an NSException.
If that's the case, you can catch it and handle it. It looks like it might format a useful message telling you what's wrong with with expressions.
#try
{
// do work
}
#catch(NSException* ex)
{
// handle
}
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html
Also, look to see if those libraries offer functions that pass in a ref to NSError. If so, you can use that.
There's also DDMathParser which is supposed to be a modern math parser and it looks like it takes NSError. Might be worth a look.
http://github.com/davedelong/DDMathParser

copying object from NSMutableArray to another using IF statement

OK, maybe I'm not seeing clear anymore and hope you can help.
I'm trying to select an Object from a NSMutableArray using:
if([car.seat isEqualToString:#"fancyOne"]){
fancyThings = [[NSMUtableArray]init];
[fancyThings addObjects: car];
}
Now I forgot to tell you I'm new at this Objective-C, so maybe I'm thinking the wrong way.
What I'm basically trying to do is to get an Object from one array by selecting a value of it's components.
This is the way to do it, I am however keep having trouble with my if-statement.
If I leave out the IF-statement it does fill my other NSMutableArray with the exact same object (thisCar) but if I put in the IF-statement it doesn't pick up that the string is the same in thisCar.seat.
I next example it puts everything in the normalThings but there are some aCar.seats which contain the string FANCYONE. I checked the XML file on spaces and that sort of things but everything is in order as far as I can see.
Shall I build it using NSScanner instead of IsEqualToString?
- (void)viewDidLoad {
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.fancyThings = [[NSMutableArray alloc]init];
for (CARS *aCar in appDelegate.someCars) {
if ([aCar.seats isEqualToString:#"FANCYONE"]){
[appDelegate.fancyThings addObject:aCar];
}
else {
[appDelegate.normalThings addObject:aCar];
}
}
self.title = #"Cars";
super viewDidLoad];
}
EDIT:
My BAD!! The code supplied was in fact in order!
There was a mistake in my XMLParser, which added blank lines to the strings, so I couldn't get an equal string!
Hopefully this will give you some guidance:
//init new array
NSMutableArray *fancyThings = [[NSMutableArray alloc] init];
//walk your array
for (SomeCarObject *thisCar in arrayOfCars) {
//is thisCar a qualifying object
if ([thisCar.seat isEqualToString:#"fancyOne"]) {
//yes, add thisCar object
[fancyThings addObject:thisCar];
}
}
You'll want to create that NSMutableArray outside of the for loop (assuming you're iterating through a collection). Then you can add to that NSMutableArray like you did.
Hope this helps!
BTW, you should edit your question with the comment you made to elaborate on it..
It's depends from volume of objects, which u deal with. If there is 1000 objects or less, this method looks good. But if there is more objects, u have risk to freeze u application and have a big memory leaks.
Also if u will need concurrency code later, u have to keep in u mind some
other solutions.
U can using not just a string objects in u array, u can try to fill u array after application startup in objects, which response if string is same or not. Or using nsdictionary with appropriate keys.
Please read my post multithread search design

While-Loop works but not the if statement?

I have this while loop in my code. The loop seems to work fine since I printed my i++ in the console. But for some reason it only checks my if statement the first time around. I can only add one title into the NSMutableArray called sectionZeroTitleArray. I have many arrays in this loop so it might get confusing. I will try my best to explain.
Here is what I am trying to do:
Loop through the length of an array(topicArray). If the array's(topicArray)is the same as this other array's(anotherArray) first object then add an object that has the same index(titleArray) as topicArray to a new MutableArray(sectionZeroTitleArray).
I'm sure I did something stupid, maybe someone that hasn't stared at this all day can fix me up? Please and thank you.
while (i < (topicArray.count)) {
if ([topicArray objectAtIndex:i] == [anotherArray objectAtIndex:0]) {
[sectionZeroTitleArray addObject:[titleArray objectAtIndex:i]];
}
NSLog(#"sectionZeroLoopCount: %d", i);
i++;
}
You are checking for pointer equality when you use ==. Are you sure you want to do this? What is the type that you're expecting? If it's a NSString, use isEqualToString:, otherwise use NSObject's isEqual: method:
If the expected type is an NSString:
if([[topicArray objectAtIndex:i] isEqualToString:[anotherArray objectAtIndex:0]]) {
//...
}
Otherwise, you should probably do this:
if([[topicArray objectAtIndex:i] isEqual:[anotherArray objectAtIndex:0]]) {
//...
}
Yeah, you're comparing the pointers and not the values. Look at the documentation of NSString, particular the isEqualToString: method for comparing strings.

Is there an tutorial that shows how attribute validation works in Core Data?

I'd like to play around with attribute value validation, but the documentation is pretty empty on this. Maybe there's an good article or tutorial out there?
Here is a fairly common validation to ensure you don't get nonsensical dates put into a timeStamp.
- (BOOL)validateTimeStamp:(id *)valueRef error:(NSError **)outError
{
NSDate *testDate=(NSDate *) valueRef;
if ([testDate compare:self.minimumTimeStamp]==NSOrderedAscending) {
// generate and return error so you can set a proper date
}
return YES;
}