iOS 8 for Programmers: An App-Driven Approach with Swift (3rd Edition) (Deitel Developer Series) by Paul J. Deitel & Harvey M. Deitel & Abbey Deitel

iOS 8 for Programmers: An App-Driven Approach with Swift (3rd Edition) (Deitel Developer Series) by Paul J. Deitel & Harvey M. Deitel & Abbey Deitel

Author:Paul J. Deitel & Harvey M. Deitel & Abbey Deitel
Language: eng
Format: azw3
Publisher: Pearson Education
Published: 2014-12-15T06:00:00+00:00


* * *

Fig. 5.16 | Model initializer and regionsChanged method.

Using NSBundle to Get Filenames

Recall that Foundation framework class NSBundle enables an app to access resources that are bundled with the app. Lines 52–53 obtain an Array of Strings containing the paths to all of the PNG images stored in the app’s main bundle. NSBundle method mainBundle returns the app’s main bundle. NSBundle method pathsForResourcesOfType returns an NSArray containing NSString representations of the paths for every file with the type specified by the first argument in the directory specified by the second argument. If the second argument is nil (as in line 53), pathsForResourcesOfType searches for all matching files in the app’s main bundle. We cast the returned NSArray of NSStrings to a Swift String Array—recall that this is allowed because the NSArray and NSString types are bridged to Swift’s Array and String types by the runtime. Next, lines 56–60 obtain the PNG image filenames from each path by using NSString property lastPathComponent. For each one, line 57 uses String method hasPrefix to determine whether that filename starts with "AppIcon"—the name Xcode gives to the app icons when you drag them onto the app’s asset catalog. Our app icons are also PNG images, so we add a filename to allCountries only if it does not start with "AppIcon". Finally, line 62 calls method regionsChanged to populate the countriesInEnabledRegions Array that’s used by the QuizViewController to select guesses to display with each flag.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.