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

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;
}

Related

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

Implementing a search suggestions for iPhone app

I have a list of foods (e.g. apple, pear, banana, etc.). Suppose a user type "appl", I want my app to return the top 5 (the number 5 is arbitrary) closest results within this list. What is a good algorithm/data structure for implementing this?
You should get all the data from the database and store it in an array and then if you are using search bar -
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
use this method and apply logic to search something like
for(NSString *curString in backUpArayForAllFiles) {
flagCountForSearch++;
if ([curString rangeOfString:substring options:NSCaseInsensitiveSearch].location == NSNotFound)
{
}
else
{
}
The usual way is using a UISearchDisplayController to implement the UISearchDisplayDelegate protocol - and use NSPredicate (NSFetchResultsController) to filter through the table(s).
I think this may help : NSFetchedResultsController with search

How to write a value validation method for core data?

The docs say:
you should implement methods of the
form validate:error:, as defined by the NSKeyValueCoding protocol
so lets say I have an attribute which is an int: friendAge
I want to make sure that any friend may not be younger than 30. So how would I make that validation method?
-validateFriendAge:error:
What am I gonna do in there, exactly? And what shall I do with that NSError I get passed? I think it has an dictionary where I can return a humanly readable string in an arbitrary language (i.e. the one that's used currently), so I can output a reasonable error like: "Friend is not old enough"... how to do that?
You can do anything you want in there. You can validate that the age is between ranges or any other logic you want.
Assuming there is an issue, you populate the error and have at least a value for the NSLocaliedDescriptionKey. That error will then be handed back to the UI or whatever this value is getting set from and allow you to handle the validation error. This means if there is other useful information you may need in the calling/setting method, you can add it into the NSError here and receive it.
For example:
-(BOOL)validateFreindAge:(NSNumber*)ageValue error:(NSError **)outError
{
if ([ageValue integerValue] <= 0) {
NSString *errorDesc = #"Age cannot be below zero.";
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:errorDesc forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:#"MyDomain" code:1123 userInfo:dictionary];
return NO;
}
return YES;
}

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

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.

how to write an If statement, to compare core data attribute value?

i'm an objective-c newcomer.
im trying to compare a core data entity attribute value and having trouble with the syntax.
i just want to know the best way way to write the if statement to compare values.
in this example, the someAttribute attribute is a boolean, and its default is NO.
NSString *myValue = [NSString stringWithFormat:#"%#", myObject.someAttribute];
if ([myValue isEqualToString:#"1"]) {
// do something
} else {
// do something else
}
is this the right way? i've tried other flavors, like below, but the results aren't accurate:
if (myObject.someAttribute == 1) {
if (myObject.someAttribute) {
If you look in the generated header for this entity, there's a good chance that the actual type of the property is not BOOL, but NSNumber, which is how Cocoa boxes numeric types into objects. Assuming I'm right, you might try:
if ([myObject.someAttribute boolValue]) { ... }
If your attribute is of BOOL type, this code will work fine
if(myObject.someAttribyte){
//so smth if someAttribute is YES
}
You can't convert a BOOL directly to a string.
Predicates are the preferred method of comparing CoreData values. It's more complicated to start but works better in the long run. See NSPredicate programming guide