tailieunhanh - Phát triển ứng dụng cho iPhone và iPad - part 20
Tham khảo tài liệu 'phát triển ứng dụng cho iphone và ipad - part 20', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Creating the Tasks Model 159 The NSError object that you return is a pointer to a pointer. This is a consequence of the fact that Objective-C passes all objects by value. If you passed the NSError pointer as a regular pointer you would only be able to modify the NSError that you passed into the method. Accepting a pointer to a pointer allows you to pass back a completely different NSError object than the one that you passed into the method. Therefore instead of changing the values in the object that you passed in you can create a new NSError with your own values to return. Implementing error methods work when you are trying to validate the value for one field. Suppose however that you need to validate multiple fields simultaneously because they depend on each other s value. Imagine that you wanted to make the details attribute required only when the price of a product was greater than 1. The framework calls two methods after all of the single field validation methods are completed. These are validateForInsert for inserting new objects and validateForUpdate for updates. As in the single field validation these functions return a BOOL indicating whether validation is successful. You could write a function to check the price field and return NO if the price is greater than 1 and the details field is empty. Then you could put a call to this function in the validateForInsert and validateForUpdate methods so that the rule runs any time a new object is inserted into Core Data or when an existing object is modified. Implementing Default Values As you have seen with custom validation rules it is possible to code rules that are more complex than what you can create in the modeling tool. You can apply the same principle to implementing default values for the attributes of your entity. Most of the data types allow you to hard-code in a default value in the modeling tool. However suppose that you want to dynamically determine the default value at runtime. You could
đang nạp các trang xem trước