Database Reference
In-Depth Information
}
That's quite a bit of code, but it's all fairly simple. The first method,
insertSampleData , checks for any bow ties (you'll learn how this works later) and if
none are present, it grabs the bow tie information in SampleData.plist , iterates
through each bow tie dictionary and inserts a new Bowtie entity into your Core Data
store. At the end of this iteration, it saves the managed context property to commit
these changes to disk.
The second method, colorFromDict , is also simple. SampleData.plist stores colors
in a dictionary that contains three keys: red, green and blue. This method takes in
this dictionary and returns a bona fide UIColor .
Notice two things:
1. The way you store images in Core Data. The property list contains a file
name for each bow tie, not the file imageā€”the actual images are in the project's
asset catalog. With this file name, you instantiate the UIImage and immediately
convert it into NSData by means of UIImagePNGRepresentation() before storing it in
the imageData property.
2. The way you store the color. Even though the color is stored in a
transformable attribute, it doesn't require any special treatment before you store
it in tintColor . You simply set the property and you're good to go.
The previous methods insert all the bow tie data you had in SampleData.plist into
Core Data. Now you need to access the data from somewhere!
Replace viewDidLoad with the following implementation:
override func viewDidLoad() {
super . viewDidLoad ()
//1
insertSampleData ()
//2
let request = NSFetchRequest (entityName: "Bowtie" )
let firstTitle = segmentedControl . titleForSegmentAtIndex ( 0 )
request. predicate =
NSPredicate (format: "searchKey == %@" , firstTitle!)
//3
var error: NSError ? = nil
var results =
managedContext . executeFetchRequest (request,
error: &error) as [ Bowtie ]?
Search WWH ::




Custom Search