13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8: Data Management with Core Data 239Table 8-2. Managed Object Validation MethodsMethod-(BOOL)validateForDelete:(NSError**)error-(BOOL)validateForInsert:(NSError**)error-(BOOL)validateForUpdate:(NSError**)errorDescriptionDetermines whether the receiver can be deletedfrom the object store in its current state. The defaultimplementation performs basic checking based onthe presence or absence of values.Determines whether a new object can be insertedinto a data store for the first time.Determines whether some changes made to anobject are valid. This is called when the object is tobe written out to persistent storage.Most of the time your validation logic will be based on particular values, and thus will be partof the data model. For example, for numeric values you can specify a minimum and maximumallowed value. If you want only multiples of 100, then you need to write your own validationmethods in a custom subclass. For strings, you can provide minimum and maximum lengthpredicates as well as a format predicate in the form of a regular expression. For all (or almost all)properties you can also provide a default value, which will be used if no other is supplied whenthe object is first inserted into a managed object context.Update the model to add some validation and some default values. Firstly, in the model editor,select the label attribute of the Address entity, and in the Core Data Inspector, set its defaultvalue to “Home.” Since this is a required attribute, it is very useful to supply a default value—this way, when a new object is created so it can be displayed for editing, everything will workas expected. Without the default value, any automatic creation by an object controller for thispurpose would fail with a validation error.There are some other required attributes: the email property of EmailAddress and thephoneNumber property of the PhoneNumber entity. Give these default values of “johnnyappleseed@me.com” and “(123) 555-1234,” respectively. Also, the Person entity’s lastName attribute isrequired: set a default value here, too (we used firstName = “John” and lastName = “Appleseed”because we’re both huge Apple geeks and thus like to copy Apple’s usual example).You can implement validation on the e-mail address and phone number using regularexpressions (often called regex for short). The full syntax of regular expressions is easilyavailable online, and is also rather large and complex, so please forgive us as we gloss overthem a little here. First, select the email attribute of the EmailAddress entity and set its regularexpression (labeled “Reg. Ex.” in the inspector) to the value in Listing 8-7.Listing 8-7. An Email Validation Regular Expression^[A-Za-z0-9._% + −] + @([A-Za-z0-9.-] + \.) + [A-Za-z]{2,4}$Let’s break this apart to learn a little about the syntax of regular expressions and to see how it isused to validate e-mail addresses.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!