tag:blogger.com,1999:blog-39749243220712216972024-03-13T22:29:00.951-07:00iPhone Programming DiscoveriesSharing my solutions and findings to the problems I run into while programming iPhone apps.Unknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-3974924322071221697.post-45308322407718361042017-02-24T15:55:00.001-08:002017-02-24T15:55:24.818-08:00Lightweight Migration ExampleMy existing app TasteBank is a personal taste tracking app. It originally allowed you to enter a list of restaurants and track details about the dishes you ate at that restaurant. I decided I wanted to make it a more generic taste tracker. I wanted to add categories to the app so you could track your tastes not just in dishes at restaurants, but also your taste in wines, or coffee.<br />
<br />
Here is the core data diagram for before:<br />
<a href="http://2.bp.blogspot.com/-C_s3GNnTgXc/WIflAdy7OfI/AAAAAAAAB0A/Orfg4OPQxM0sX7NmW4En6EDmMvnmBoCKQCK4B/s1600/Screen%2BShot%2B2017-01-24%2Bat%2B3.34.01%2BPM.png" imageanchor="1"><img border="0" height="225" src="https://2.bp.blogspot.com/-C_s3GNnTgXc/WIflAdy7OfI/AAAAAAAAB0A/Orfg4OPQxM0sX7NmW4En6EDmMvnmBoCKQCK4B/s400/Screen%2BShot%2B2017-01-24%2Bat%2B3.34.01%2BPM.png" width="400" /></a><br />
<br />
And after:<br />
<a href="http://2.bp.blogspot.com/-N8ln6Wdheik/WIflJODcVpI/AAAAAAAAB04/Aq9DsyOnkQU0AqZBeRRI27QivHjvWrB-QCK4B/s1600/Screen%2BShot%2B2017-01-24%2Bat%2B3.35.55%2BPM.png" imageanchor="1"><img border="0" height="222" src="https://2.bp.blogspot.com/-N8ln6Wdheik/WIflJODcVpI/AAAAAAAAB04/Aq9DsyOnkQU0AqZBeRRI27QivHjvWrB-QCK4B/s400/Screen%2BShot%2B2017-01-24%2Bat%2B3.35.55%2BPM.png" width="400" /></a><br />
<br />
<br class="Apple-interchange-newline" />
So I basically added 1 new entity called Category and some new attributes.<br />
<br />
According to <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html" target="_blank">apple docs</a>, in order for me to merge these two different version of my data model, I would need to perform a lightweight migration. Adding attributes or entities can usually be done using lightweight migrations.<br />
<br />
I wanted to make sure that the existing data would not be overwritten and that the list of restaurants and dishes would just be moved to a category called Restaurants.<br />
<br />
The following is a step by step list of what I had to do to complete this successfully. There are many examples of the first few steps on the internet already, the last few steps are specific to my data.<br />
<br />
<h3>
<b>Create a new database model version:</b></h3>
1. Click on my database .xcdatamodeld file in project navigator<br />
2. Go to Editor in menu bar, and select Add Model Version...<br />
3. Name the version of data model, I used TastebankVer2 and hit Finish<br />
4. A little green arrow shows which version of the data model is currently being used, to make the new version the current active version, you click on the data model you just added, then go to utilities->Model Version->select current version in drop down list<br />
<br />
<div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-ijGKerYbeG4/WJeh_MoK9yI/AAAAAAAACQU/YC2DB1fL6NAkGz34rvDT7Hmmre2mrgXWgCK4B/s1600/Screen%2BShot%2B2017-02-05%2Bat%2B2.04.36%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="38" src="https://4.bp.blogspot.com/-ijGKerYbeG4/WJeh_MoK9yI/AAAAAAAACQU/YC2DB1fL6NAkGz34rvDT7Hmmre2mrgXWgCK4B/s640/Screen%2BShot%2B2017-02-05%2Bat%2B2.04.36%2BPM.png" width="640" /></a></div>
<br />
<br />
<h3>
<b>Add code to enable lightweight migration:</b></h3>
<br />
1. Open App delegate file<br />
2. Go to <a class="urlLink" href="https://developer.apple.com/reference/coredata/nspersistentstorecoordinator/1468860-addpersistentstorewithtype" style="background-color: white; color: #3366cc; font-family: Courier, Consolas, monospace; font-size: 13.39px;" target="_self">addPersistentStoreWithType:configuration:URL:options:error:</a> method<br />
<br />
<a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html" target="_blank">Apple's</a> suggested code shows:<br />
<br />
<br />
<br />
<section><a href="https://www.blogger.com/null" name="//apple_ref/doc/uid/TP40004399-CH4-SW3" style="background-color: white; color: #3366cc; font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif; font-size: 13px;" title="Use a Migration Manager if Models Cannot Be Found Automatically "></a></section><br /><br />
<br />
<section><div class="codesample clear" style="clear: both; font-size: 11.9171px; margin: 1.5em 0px;">
<table style="background-color: #f1f5f9; border-collapse: collapse; border: 1px solid rgb(199, 207, 213); padding-bottom: 4px; width: 1010px;"><tbody>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">NSError *error = nil;<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">NSURL *storeURL = <#The URL of a persistent store#>;<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">NSPersistentStoreCoordinator *psc = <#The coordinator#>;<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;"> [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;"> [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;"> <span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">BOOL success = [psc addPersistentStoreWithType:<#Store type#><span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;"> configuration:<#Configuration or nil#> URL:storeURL<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;"> options:options error:&error];<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">if (!success) {<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;"> // Handle the error.<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
<tr style="border-bottom: 0px solid rgb(255, 255, 255);"><td scope="row" style="padding: 0.5em;"><pre style="color: #666666; font-family: Courier, Consolas, monospace; font-size: 12.2746px; line-height: 13.5021px; margin-left: 0.5em; margin-right: 0.333em; margin-top: -0.083em; white-space: pre-wrap;">}<span style="float: right; margin-right: 0.667em;"></span></pre>
</td></tr>
</tbody></table>
</div>
<div style="font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif; font-size: 13px; font-stretch: normal; line-height: normal; margin-bottom: 0.833em;">
<br /></div>
<div style="font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif; font-size: 13px; font-stretch: normal; line-height: normal; margin-bottom: 0.833em;">
Here is my persistentStoreCoordinator code:</div>
<div style="font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif; font-size: 13px; font-stretch: normal; line-height: normal; margin-bottom: 0.833em;">
<br /></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;">- (</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">NSPersistentStoreCoordinator</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> *)persistentStoreCoordinator</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;">{</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s4" style="font-variant-ligatures: no-common-ligatures;">if</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> (</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">_persistentStoreCoordinator</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> != </span><span class="s4" style="font-variant-ligatures: no-common-ligatures;">nil</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s4" style="font-variant-ligatures: no-common-ligatures;">return</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">_persistentStoreCoordinator</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">;</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">NSError</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> *error = </span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">nil</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">;</span></div>
<div>
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">NSURL</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> *storeURL = </span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">[[</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">self </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">applicationDocumentsDirectory</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">] </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">URLByAppendingPathComponent</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:</span><span class="s6" style="font-variant-ligatures: no-common-ligatures;">@"TasteB.sqlite"</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">];</span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;"> _persistentStoreCoordinator</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> = [[</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">NSPersistentStoreCoordinator</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">alloc</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">]</span><span class="s1" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">initWithManagedObjectModel</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">: [</span><span class="s4" style="font-variant-ligatures: no-common-ligatures;">self</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s8" style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">managedObjectModel</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">]];</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> BOOL success = [ </span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">_persistentStoreCoordinator </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;"> addPersistentStoreWithType</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:</span><span class="s1" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">NSSQLiteStoreType</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;"><span style="color: black;"> </span>configuration</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:</span><span class="s4" style="font-variant-ligatures: no-common-ligatures;">nil</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">URL</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:storeURL </span></div>
<div class="p6" style="color: #3e1e81; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s5" style="font-variant-ligatures: no-common-ligatures;"><span style="color: black;"> </span>options</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:</span><span class="s9" style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">@{</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">NSMigratePersistentStoresAutomaticallyOption</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:</span><span class="s9" style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">@YES</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">, </span></div>
<div class="p6" style="color: #3e1e81; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> NSInferMappingModelAutomaticallyOption</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:</span><span class="s9" style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">@YES}</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="font-variant-ligatures: no-common-ligatures;">error</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">:&error];</span></div>
<div class="p6" style="color: #3e1e81; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">if</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> (!success) {</span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">NSLog</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="s6" style="color: #d12f1b; font-variant-ligatures: no-common-ligatures;">@"Unresolved error %@, %@"</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">, error, [error </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">userInfo</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">]);</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">abort</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">();</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s4" style="font-variant-ligatures: no-common-ligatures;">return</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">_persistentStoreCoordinator</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">;</span></div>
<div style="font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif; font-size: 13px; font-stretch: normal; line-height: normal; margin-bottom: 0.833em;">
</div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;">}</span></div>
</section><br />
At this point the lightweight migration is activated and should work right away for adding attributes and stuff. <br />
<br />
<h3>
Copy old data into new structure</h3>
<div>
However I also wanted my existing data restructured. I already had a list of restaurants and dishes in the old version of my app. <br />
<br />
<a href="http://2.bp.blogspot.com/-nhlrA6zjWGY/WLDGI6Dk5AI/AAAAAAAAC9g/A3FMqcw71Oo5EPUDoDoQvBHl5wAsp1qVQCK4B/s1600/IMG_3231.PNG" imageanchor="1"><img border="0" height="320" src="https://2.bp.blogspot.com/-nhlrA6zjWGY/WLDGI6Dk5AI/AAAAAAAAC9g/A3FMqcw71Oo5EPUDoDoQvBHl5wAsp1qVQCK4B/s320/IMG_3231.PNG" width="213" /></a> ----------> <a href="http://2.bp.blogspot.com/-4oIhv5w0c4w/WLDGZbLypwI/AAAAAAAAC9o/jo30dJxkLA0faTcFJXFXzdrXvq-qd7GCwCK4B/s1600/IMG_3230.PNG" imageanchor="1"><img border="0" height="320" src="https://2.bp.blogspot.com/-4oIhv5w0c4w/WLDGZbLypwI/AAAAAAAAC9o/jo30dJxkLA0faTcFJXFXzdrXvq-qd7GCwCK4B/s320/IMG_3230.PNG" width="213" /></a><br />
<br /><br />
<br />
When moving to the new version, I wanted all of this data moved under a new category called Restaurants, and I needed to update the category attribute on the Restaurant entities.</div>
<div>
<a href="http://4.bp.blogspot.com/-EsiEf2eX1sI/WJoj4xvIWdI/AAAAAAAACRE/tWWL1_y0m-cmJMIkqfnMIF5sHfVSAtssgCK4B/s1600/Screen%2BShot%2B2017-02-07%2Bat%2B11.45.06%2BAM.png" imageanchor="1"><img border="0" height="180" src="https://4.bp.blogspot.com/-EsiEf2eX1sI/WJoj4xvIWdI/AAAAAAAACRE/tWWL1_y0m-cmJMIkqfnMIF5sHfVSAtssgCK4B/s200/Screen%2BShot%2B2017-02-07%2Bat%2B11.45.06%2BAM.png" width="200" /></a> -----> <a href="http://4.bp.blogspot.com/-dLsLNx3DbyI/WJojl2Co9lI/AAAAAAAACQ8/q043W5N7Z_UOTAE8Xkz9Tw_pG1yy2LZwwCK4B/s1600/Screen%2BShot%2B2017-02-07%2Bat%2B11.43.38%2BAM.png" imageanchor="1"><img border="0" height="200" src="https://4.bp.blogspot.com/-dLsLNx3DbyI/WJojl2Co9lI/AAAAAAAACQ8/q043W5N7Z_UOTAE8Xkz9Tw_pG1yy2LZwwCK4B/s200/Screen%2BShot%2B2017-02-07%2Bat%2B11.43.38%2BAM.png" width="185" /></a> ------> <a href="http://3.bp.blogspot.com/-Z74-K2BaAiA/WLDHmZ6zjHI/AAAAAAAAC90/t4mzflD4QGoBorM1jMAA_Qr719NiOAVnQCK4B/s1600/screenshot_iphone5.png" imageanchor="1"><img border="0" height="200" src="https://3.bp.blogspot.com/-Z74-K2BaAiA/WLDHmZ6zjHI/AAAAAAAAC90/t4mzflD4QGoBorM1jMAA_Qr719NiOAVnQCK4B/s200/screenshot_iphone5.png" width="116" /></a></div>
<div>
<br /></div>
<div>
I put this code in my App delegate:</div>
<div>
<br /></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;">-(</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">void</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">)makeCategory{</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">NSError</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> *error = </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">nil</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">;</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">NSFetchRequest</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> *fetch = [[</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">NSFetchRequest</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">alloc</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">] </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">initWithEntityName</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">:</span><span class="s6" style="color: #d12f1b; font-variant-ligatures: no-common-ligatures;">@"Category"</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">];</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">NSUInteger</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> numberOfcategories = [</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">self</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">managedObjectContext</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">countForFetchRequest</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">:fetch </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;"> error</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">:&error];</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> //if there are no existing categories, then create a new one called Restaurants</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">if</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">( numberOfcategories == </span><span class="s8" style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">0</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> ){</span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">NSLog</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">@"Categories not found, creating Restaurant Category"</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">);</span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div class="p5" style="color: #008400; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">Category</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> *c=[</span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">NSEntityDescription</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">insertNewObjectForEntityForName</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">:</span><span class="s6" style="color: #d12f1b; font-variant-ligatures: no-common-ligatures;">@"Category"</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p5" style="color: #008400; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;"> inManagedObjectContext</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">:</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">self</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">managedObjectContext</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">];</span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> c.</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">name</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">=</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">@"Restaurants"</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p5" style="color: #008400; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">//if there are existing restaurants then set their category attribute to point to the new //Restaurant category</span></div>
<div class="p5" style="color: #008400; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div class="p3" style="color: #703daa; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">NSFetchRequest</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> *fetch1 = [[</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">NSFetchRequest</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">alloc</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">] </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">initWithEntityName</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">:</span><span class="s6" style="color: #d12f1b; font-variant-ligatures: no-common-ligatures;">@"Restaurants"</span><span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;">];</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">if</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> ([</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">self</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">managedObjectContext</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">countForFetchRequest</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">:fetch1 </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">error</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">:&error] ></span><span class="s8" style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">0</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div class="p5" style="color: #008400; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p5" style="color: #008400; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> //make the new restaurant category relation</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s3" style="color: #703daa; font-variant-ligatures: no-common-ligatures;">NSArray</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> *fetchedObjs= [</span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">self</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">managedObjectContext</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;">executeFetchRequest</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">:fetch1 </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s5" style="color: #3e1e81; font-variant-ligatures: no-common-ligatures;"> error</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">:&error];</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">for</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> (</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">Restaurants</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> *r </span><span class="s2" style="color: #ba2da2; font-variant-ligatures: no-common-ligatures;">in</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;"> fetchedObjs) {</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> r.</span><span class="s7" style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">category</span><span class="s1" style="font-variant-ligatures: no-common-ligatures;">=c;</span></div>
<div class="p4" style="color: #d12f1b; font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s4" style="color: black; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div class="p2" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 13px;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div class="p1" style="font-family: Menlo; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="s1" style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div>
<span style="font-family: "menlo"; font-size: 11px;">}</span></div>
<div>
<br /></div>
<div>
I called this function from <span style="font-family: "menlo"; font-size: 11px;">didFinishLaunchingWithOptions: method .</span> </div>
<div>
<br /></div>
<div>
<br /></div>
<div>
References:</div>
<div>
https://the-nerd.be/2012/02/05/how_to_do_a_lightweight_core_data_migration/</div>
<div>
https://www.objc.io/issues/4-core-data/core-data-migration/</div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}
span.s1 {font-variant-ligatures: no-common-ligatures}
</style>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-3974924322071221697.post-3030213899817879402013-11-09T15:53:00.002-08:002013-11-09T15:56:38.504-08:00Renewing UILocalNotifications<h1 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="font-family: Arial; font-size: 15px; font-weight: normal; line-height: 1.15; white-space: pre-wrap;">(using iOS 7 XCode 5)</span></h1>
<b id="docs-internal-guid-6b881666-3f41-9bed-3847-0a33be5f9257" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">I am writing an app where I need to send the user a message at the same time everyday, indefinitely (unless the user cancels). Alert messages can be scheduled with the OS using UILocalNotifications, for details on this check out the </span><a href="https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">Apple documentation</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">. </span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">It sounds simple enough: configure a local notification to appear every day at a time of the user’s choosing. I could use the repeatInterval property which allows the app to schedule a daily alert. But I want the user to receive a different message everyday and the repeatInterval property will show the same message body everyday.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">So I have to schedule each alert separately each with a different message, and time. Also note that there is a maximum number of notifications that can be scheduled at a given time (64 according to documentation). So in order to schedule notifications that span over months either the app has to automatically keep adding notifications at a later date or the user has to manually reschedule them after 64 days. I prefer the first solution where the app figures out that an alert has been shown and adds a new notification to the queue. But for this to happen I would need a background process running even when app is not in the foreground. According to my Googling, this is not allowed by the OS (to be certain I asked </span><a href="http://stackoverflow.com/questions/14821564/setting-random-localnotifications-on-iphone" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">stackoverflow</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">). The second option seems cumbersome for the user.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br class="kix-line-break" />My solution includes both of the options, having the user initiate the rescheduling by opening the app from the alert and having the app automatically reschedule when it is launched. When an alert is shown, the action button says ‘go to app’ and it launches the app. This ‘go to app’ action will take the user to the main page of the app, in the background, the app figures out how many local notification slots are available and then adds more notifications to the schedule. This solution needs the user to click one button, and the app does the rest. (When shown in a locked screen the user just needs to slide the alert and is taken to the app, again the app reschedules at that point.) </span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">I will break this up into two parts: </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">1. scheduling daily alerts (64 total) </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">2. adding to schedule in the background when app is launched. </span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Scheduling daily alerts</span></h2>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">In my app I used a time picker to let the user choose the time of day they would like to receive their daily message and had them determine the content. However for the sake of this example, let’s assume that the current time is used to schedule the notifications, and the alert messages are contained in an array of strings called </span><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">userDefinedMessages</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Here is how I configure a local notification that repeats every day with a different alert message for 64 days:</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//Create a calendar</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">NSCalendar *currentCalendar = [NSCalendar currentCalendar];</span></div>
<b style="font-weight: normal;"><br /><a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/DatesAndTimes/Articles/dtCalendars.html" style="text-decoration: none;"><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></a></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//Create a day component offset, days will be incremented by 1</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">NSDateComponents *dayComponent = [[NSDateComponents alloc] init] ;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">[dayComponent setDay:1];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//Create date from current date so notifications start being scheduled from current time details </span><a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/DatesAndTimes/Articles/dtCalendars.html" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">here</span></a><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">NSDate *startDate =[NSDate date];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//Configure 64 local notifications, increment day component every time</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">for(int i =0;i<64;i++) {</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">UILocalNotification *localNotif = [[UILocalNotification alloc] init];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.timeZone = [NSTimeZone defaultTimeZone];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//content of the alert message are set from an array of messages set by user, not shown here</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.alertBody = [userDefinedMessages objectAtIndex:i];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.alertAction = @”Go to App”;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//set the alert schedule date </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> startDate = [currentCalendar dateByAddingComponents:dayComponent toDate:startDate options:0];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.fireDate = startDate;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.soundName = UILocalNotificationDefaultSoundName;</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr">
<table style="border-collapse: collapse; border: none;"><colgroup><col width="624"></col></colgroup><tbody>
<tr style="height: 5px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><h2 dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Renewing Local Notifications</span></h2>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">When a local notification fires, whether the alert launches the app or the app is already in the </span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">foreground, the </span><a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">didReceiveLocalNotification</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> method will be called. This can be added to the </span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">AppDelegate.m file. In my case, when the app is launched from an alert, I check to see how </span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">many local notifications are left in the queue, if there is room, I add more.</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">- (void)application:(UIApplication *)application</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">didReceiveLocalNotification:(UILocalNotification *)notification {</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//Get count of currently scheduled notifications, details </span><a href="https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">here</span></a><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">int numberOfScheduledLocalNotifications = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> int numberOfEmptyNotificationSlots = 64 - numberOfScheduledLocalNotifications;</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//Sort the scheduledLocalNotifications array by firedate so that the last notification is </span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">// first in the array. This way we can find the last notification scheduled and add new notifications after that date. </span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//This sorting code was obtained from </span><a href="http://stackoverflow.com/questions/17641678/get-time-only-of-next-ios-local-notification" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">here</span></a><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> NSSortDescriptor *sortDes = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:NO];</span><br />
<span style="font-family: Cambria; font-size: 13px; line-height: 1.0833333333333333; white-space: pre-wrap;"><br /></span>
<span style="font-family: Cambria; font-size: 13px; line-height: 1.0833333333333333; white-space: pre-wrap;">NSArray *notifArray = [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[sortDes]];</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> //Get the date alert is scheduled for from the local notifications if there are any scheduled, otherwise just use </span><br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//current date</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> NSDate *fireDate;</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> if([notifArray count]) {</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> UILocalNotification *lastNotif= [notifArray objectAtIndex:0];</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> fireDate = lastNotif.fireDate;</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> } else {</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> fireDate=[NSDate date];</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> //Create a calendar</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> NSCalendar *currentCalendar = [NSCalendar currentCalendar];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> //Create a day component offset, days will be incremented by 1</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> NSDateComponents *dayComponent = [[NSDateComponents alloc] init] ;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> [dayComponent setDay:1];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> //Configure local notifications, increment day component every time</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">for(int i =0;i<numberOfEmptyNotifiationSlots;i++) {</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">UILocalNotification *localNotif = [[UILocalNotification alloc] init];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.timeZone = [NSTimeZone defaultTimeZone];</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//content of the alert message are set from an array of messages set by user, this array can be saved in core </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//data then retrieved here or saved in </span><a href="https://developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">NSUserDefaults</span></a><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.alertBody = <get from user array>;</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">localNotif.alertAction = @”Go to App”;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> //set the alert schedule date to be a day after the last scheduled notification</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> fireDate = [currentCalendar dateByAddingComponents:dayComponent toDate:fireDate options:0];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> localNotif.fireDate = fireDate;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> localNotif.soundName = UILocalNotificationDefaultSoundName;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
</td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">This will add more notifications to the schedule when the user launches the app from the alert </span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">message. </span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.0833333333333333; margin-bottom: 0pt; margin-left: 5pt; margin-right: 3pt; margin-top: -1pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Please note that there is a way to schedule notifications using the </span><a href="https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006728-CH3-SW91" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">scheduledLocalNotification</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">which I have not used here.</span></div>
</td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">References:</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<a href="https://developer.apple.com/library/ios/DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">https://developer.apple.com/library/ios/DOCUMENTATION/NetworkingInternet/Conceptua</span></a><br />
<a href="https://developer.apple.com/library/ios/DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">l/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<a href="http://www.icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">http://www.icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<br />
<span style="background-color: #f1f5f9; color: #666666; font-family: Consolas; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: black; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: #666666; font-family: Consolas; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: #666666; font-family: Consolas; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: #666666; font-family: Consolas; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: black; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: #666666; font-family: Consolas; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: #666666; font-family: Consolas; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
<tr style="height: 0px;"><td style="border: 0px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br />
<span style="background-color: #f1f5f9; color: black; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></td></tr>
</tbody></table>
</div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-3974924322071221697.post-16881764864153121022013-10-20T19:51:00.001-07:002013-10-20T19:52:21.308-07:00Creating an inclusive list in UITableView<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="font-family: 'Trebuchet MS'; font-size: 17px; vertical-align: baseline; white-space: pre-wrap;">What I wanted to do:</span></h2>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">I was trying to figure out a way to create a multiple selection list in UITableView where more than one row can have a checkmark. I wanted to use this as a menu that would allow the user to select categories from a list and return the selected category list to the calling view controller.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">This would work like the list iOS uses in its Clock/alarm app. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Here is a short video showing what I want to do:</span></div>
<div>
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.blogger.com/video.g?token=AD6v5dygSWsGO6DJ6Xq2QlCjE6L3n9l2ZB_5gF7gngFPDM06_fnyH_6-J61PzDx217RVgL6VOimWY05OBwwmxfQ0BQ' class='b-hbp-video b-uploaded' frameborder='0'></iframe></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">I wanted to be able to select/deselect multiple rows. </span><span style="font-family: Arial; font-size: 15px; line-height: 1.15; white-space: pre-wrap;">In addition to this basic functionality, I wanted to add the capability for the user to have a ‘select all’ option where they could select or deselect the entire list.</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="font-family: 'Trebuchet MS'; font-size: 17px; vertical-align: baseline; white-space: pre-wrap;">How I did it:</span></h2>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">There are two methods that are modified to achieve this:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">didSelectRowAtIndexPath - where you handle checkmarking the current cell that is selected. Here you checkmark the cell if it isn’t marked already and take the check mark off if it is.</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">cellForRowAtIndexPath - where you handle the creation of the visible cells. Here you verify whether the cell should have a checkmark or not. ( I will explain why you need this below)</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">In my code I have a list of category names stored in an array called categoryList and another mutable array that tracks which categories are currently checkmarked. </span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">self.categoryList=[[NSArray alloc]initWithObjects:@"Achievement",@"Attitude",@"Brave",@"Character",@"Courage",@"Determination",@"Enthusiasm",@"Failure",@"Fame", @"Friendship", @"Happiness",@"Inspirational",@"Leadership", @"Love", @"Motivational",@"Opportunity",@"Perseverance", @"Spiritual", nil];</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> self.selectedCategoryArray =[[NSMutableArray alloc]init];</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"> I initialize these in ViewDidLoad.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Category names are displayed in a dynamic cell UITableView.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">To make a simple inclusive selection list, you can follow the example on the Apple developer </span><a href="https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html#//apple_ref/doc/uid/TP40007451-CH9-SW10" style="text-decoration: none;"><span style="color: #1155cc; font-family: Arial; font-size: 15px; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">site</span></a><span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"> which shows:</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">{</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //get cell info</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if (cell.accessoryType == UITableViewCellAccessoryNone) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">This code simply adds a checkmark to the current cell if it doesn’t have one already and takes the checkmark off if it does. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">If you run this code you will have the ability to check and uncheck multiple items in your list.</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">However you will notice a problem when you scroll to check or uncheck items that are at the bottom of your list, you might see that the checkmarks travel down, and items you did not select are checkmarked and vice versa. This happens because cells are reused when you scroll down and the accessory information does not hold when this happens. </span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">In order to ensure that the right cells are always checked, you need to add/remove a checkmark from the cell every time it is created by cellForRowAtIndexPath. To do this, you need to track the selection or deselection of cells using an array, then check that array in cellForRowAtIndexPath to confirm that the cell should have a checkmark. </span><span style="font-family: Arial; font-size: 15px; line-height: 17px; white-space: pre-wrap;"> I will show this code after adding a few lines to didSelectRowAtIndexPath that will track this info in selectedCategoryArray:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">{</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if (cell.accessoryType == UITableViewCellAccessoryNone) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //store the selected category in array</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray addObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<br />
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //remove selected category from array</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray removeObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<br />
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"><span style="font-family: Arial; font-size: 15px; line-height: 17px;">I add the selected cell's category name into the selectedCategoryArray or remove the category name depending on whether there is a checkmark already in the cell or not.</span></span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">When the cell is created in cellForRowAtIndexPath, I make a decision about the checkmark by checking to see whether it is selected ( if the selectedCategoryArray contains the category name of the cell being created at this indexPath.row) :</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">{</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">if([self.selectedCategoryArray count]) {</span></div>
<br />
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if([self.selectedCategoryArray containsObject:[self.categoryList objectAtIndex:indexPath.row]]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } else {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self configureCell:cell atIndexPath:indexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> return cell;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Now, I have a fully functioning inclusive list in which I can select/deselect multiple categories. </span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="font-family: 'Trebuchet MS'; font-size: 17px; vertical-align: baseline; white-space: pre-wrap;">Adding “select all” capability</span></h2>
<br />
<span style="font-family: 'Trebuchet MS'; font-size: 17px; font-weight: bold; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Here is what I want the app to do:</span></div>
<br />
<span style="font-family: 'Trebuchet MS'; font-size: 17px; vertical-align: baseline; white-space: pre-wrap;"></span><img height="247px;" src="https://lh3.googleusercontent.com/NcaWUlwWiR8wX23Cq1Dtl6nLLEIdLYa77qUJ40Zdl5l0xXnP8G2wRTZmhuotiIUTrJkHArZ9ih6Nw1PK-tqrtbjuxKgKExuobqt8onWuxmNCd7rdS_l3TTx7" width="165px;" /><span style="font-family: 'Trebuchet MS'; font-size: 17px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">I want to:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">1. Add an option for the user to be able to select all rows when ‘all’ is selected. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">2. When the ‘all’ is checked I want all rows checkmarked, and all checkmarks removed when 'all' is unchecked.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">3. When ‘all’ is checked, and all rows are checkmarked, if at that time a row is clicked, then check marks in all other rows should be removed (including ‘all’) and only that row should contain the checkmark.</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Some more logic needs to be added to both the didSelectRowAtIndexPath and CellForRowAtIndexPath to do this.</span></div>
<span style="font-family: Arial; font-size: 15px; white-space: pre-wrap;"><br /></span>
<span style="font-family: Arial; font-size: 15px; white-space: pre-wrap;"><b>Step 1 : Add 'all' option</b></span><br />
<span style="font-family: Arial; font-size: 15px; white-space: pre-wrap;"><br /></span>
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">To add the ‘all’ option I simply added the string to the categoryList array, the dynamic UITableView takes care of displaying it:</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">self.categoryList=[[NSArray alloc]initWithObjects:@”all”,@"Achievement",@"Attitude",@"Brave",@"Character",@"Courage",@"Determination",@"Enthusiasm",@"Failure",@"Fame", @"Friendship", @"Happiness",@"Inspirational",@"Leadership", @"Love", @"Motivational",@"Opportunity",@"Perseverance", @"Spiritual", nil];</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><b>Step 2: 'all' checks or unchecks all rows</b></span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">To add the functionality I had to add some IF statements to see if ‘all’ is selected then handle adding checkmarks and removing checkmarks from ALL rows.</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Here is a function that loops through all the indexpaths in the table and add checkmarks to all rows:</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">-(void)checkmarkAllRows{</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //loop through all index paths and add or remove checkmark.Since we have only</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //1 section, section 0, we can have a constant for that value</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> for(int i=0;i<[self.categoryList count];i++) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //create indexpath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:i inSection:0];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //get cell info for that indexpath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:myIndexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">To take checkmarks off:</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">-(void)uncheckAllRows {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> for(int i=0;i<[self.categoryList count];i++) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:i inSection:0];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:myIndexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Here is how the didSelectRowAtIndexPath method checks if the selected category name is ‘all’ then adds/removes checkmarks from ALL rows:</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">{</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if (cell.accessoryType == UITableViewCellAccessoryNone) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray addObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //if 'all' row is selected then check all rows</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if([[self.categoryList objectAtIndex:indexPath.row] isEqualToString:@"all"]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self checkmarkAllRows];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray removeObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if([[self.categoryList objectAtIndex:indexPath.row] isEqualToString:@"all"]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self uncheckAllRows];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray removeAllObjects];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } </span><span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Remember that because of the reusing of cells, we have to account for ‘all’ in cellForRowAtIndexPath also. In this method, I just added a condition that adds a checkmark to the cell if ‘all’ is present in the selectedCategoryArray:</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">{</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if([self.selectedCategoryArray count]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-indent: 36pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> <br class="kix-line-break" />if([self.selectedCategoryArray containsObject:[self.categoryList objectAtIndex:indexPath.row]]</span><span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> || [self.selectedCategoryArray containsObject:@"all"]</span><span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } else {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self configureCell:cell atIndexPath:indexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> return cell;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Now clicking the ‘all’ cell will check/uncheck all cells!</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><b>Step 3: Additional list behavior </b></span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">I chose to make the list behave in the following way: while ‘all’ is checked, if another cell is selected, this will assume the user wants to select only that cell and so all other cells will be unchecked (Other options would be to uncheck that cell and leave the others checked so you can exclude that category, or disable selection of that cell. Depends on what kind of menu you want to create).</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">For this to work some more conditions need to be added, basically if a cell is selected and if ‘all’ is present in the selectedCategoryArray, then checkmarks from all cells should be removed and only the selected cell should be checked. </span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">{</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if (cell.accessoryType == UITableViewCellAccessoryNone) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray addObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> //if 'all' row is selected then check all rows</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if([[self.categoryList objectAtIndex:indexPath.row] isEqualToString:@"all"]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self checkmarkAllRows];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryNone;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray removeObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> if([[self.categoryList objectAtIndex:indexPath.row] isEqualToString:@"all"]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self uncheckAllRows];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray removeAllObjects];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> } </span><span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">else if([self.selectedCategoryArray containsObject:@"all"]) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; line-height: 1.15; white-space: pre-wrap;"> [self uncheckAllRows];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray removeAllObjects];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> cell.accessoryType = UITableViewCellAccessoryCheckmark;</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="color: blue; font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> [self.selectedCategoryArray addObject:[self.categoryList objectAtIndex:indexPath.row]];</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<br />
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Cambria; font-size: 13px; vertical-align: baseline; white-space: pre-wrap;">}</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">At this point, you should have a list that meets all three requirements and works like the video shown at the beginning of this post!</span></div>
<span style="font-family: Arial; font-size: 15px; line-height: 17px; white-space: pre-wrap;">I can then pass the selectedCateoryArray to a calling view controller that can use the information to configure the selections in my app.</span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">Reference</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html#//apple_ref/doc/uid/TP40007451-CH9-SW10</span></div>
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-3974924322071221697.post-69971879160139592732013-09-27T15:28:00.000-07:002013-10-20T19:56:48.210-07:00Data Persistence using PLIST<b id="docs-internal-guid-4aa01305-6186-86c4-2d2c-6caa30a219fa" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 28px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Data Persistence using PLIST</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">I was working on an app a few months ago in which I needed data persistence. I hadn’t yet mastered Core Data so I found that I could use property lists to store the data. After some reading and lots of google searches, I found that the following solution worked for me.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">This post assumes you already know your way around Xcode and basic iOS programming. This was programmed in Xcode version 4.6 for iOS5.</span></div>
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The Data</span></h2>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Here is what the data looks like in the UITableViews:</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><img height="298px;" src="https://lh6.googleusercontent.com/PRp4CuBHJQZ8VxrHYDgtxIpOqKRg4QWw5TtgLIEXmfo_ZNALNFDxCEvRTSn4yrWIGAx57uI54YwX9isOIKtUCLikO0ycR7gMS79a3qG8IgI6a6TsQM4yvaip" width="199px;" /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><img height="297px;" src="https://lh3.googleusercontent.com/Es8sp7W1PXBSZJ2MXRWPEpQAqBh3V1-YlzsqveioIcM6NLMrkdjPey8gbLTHQ-f-iwMIYOqTmoT1liqE7QjAs6hBAplUgntGAtQqeIJ2bQCX3mIF9TMXoaGG" width="170px;" /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b><br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The UITableView contains restaurants, corresponding dishes and ratings I decided to store the data in the following format:</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">I made a NSMutableArray of NSMutableDictionaries which contains the restaurant name and a NSMutableArray of dishes from the restaurant. This dish array is made up of NSMutableDictionaries that contain the dish name and dish rating. This diagram might help clarify:</span><img height="411px;" src="https://docs.google.com/drawings/d/ssReUdvWbwous4ZdT-23cJA/image?w=626&h=411&rev=970&ac=1" width="626px;" /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Plists can store data in arrays and dictionaries, so in this way I did not have to convert the data format. I simply included a plist file in the app’s Supporting Files directory, then in ViewDidLoad of my master view controller I copied the contents of the file into an array. Then modified the dictionary values within the array when the user added or deleted an item and saved it back into the file when the app exit.</span></div>
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Creating Plist file</span></h2>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">To create a plist file in XCode, go to File->New->Resources->Plist, save the file in the supporting files directory. You can then modify the plist file and add the data you would like to use in your app. In my case the plist file looked something like this:</span><img height="299px;" src="https://lh4.googleusercontent.com/5D0CBXDX4wdflN-dh4GD9nc3fwGV67V_XdC9nmEAhfjlCLAVjXWxFaes9aIyCRbpqARW3I9hH3N2kpqh5RiTAYEsbMQl45thb-cUK9utrcLcTCBReyi0Vz-3" width="333px;" /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b><br />
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Loading Plist values</span></h2>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">With the .plist file under the supporting files folder, I read in the file in the MasterViewController file in the viewDidLoad method.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//search documents directory</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">plistPath = [rootPath stringByAppendingPathComponent:@"restaurants.plist"];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//check resource directory</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> plistPath = [[NSBundle mainBundle] pathForResource:@"restaurants" ofType:@"plist"];</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> }</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">//load array</span></div>
<div dir="ltr" style="line-height: 1; margin-bottom: 0pt; margin-right: -4.5pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">self.restaurantArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">(Code referenced from </span><a href="https://github.com/bryceredd/CastleHassle/blob/master/Rev6/PListReader.mm" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">here</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">)</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Saving Plist values</span></h2>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">After adding data to my restaurant and dish dictionaries, I call this function to save the data into the documents folder.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">NSString *rootPath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> NSString *plistPath1 = [rootPath1 stringByAppendingPathComponent:@"restaurants.plist"];</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> [self.restaurantArray writeToFile:plistPath1 atomically:YES];</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Cambria; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">(Code referenced from </span><a href="https://github.com/bryceredd/CastleHassle/blob/master/Rev6/PListReader.mm" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">here</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">)</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">With this I was able to load, add, modify and save data to my plist. Modifying the data consisted of adding objects into arrays and dictionaries where needed. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Of course there are other ways of doing this, but I found this way worked for me till the next release of the app in which I used Core Data instead.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<h2 dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 10pt;">
<span style="background-color: transparent; color: black; font-family: 'Trebuchet MS'; font-size: 17px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Reference Links</span></h2>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Some more information on directories </span><a href="http://www.techotopia.com/index.php/Working_with_Directories_on_iOS_6" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">http://www.techotopia.com/index.php/Working_with_Directories_on_iOS_6</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Plist reference</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<a href="https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Code reference</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<a href="https://github.com/bryceredd/CastleHassle/blob/master/Rev6/PListReader.mm" style="text-decoration: none;"><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">https://github.com/bryceredd/CastleHassle/blob/master/Rev6/PListReader.mm</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>Unknownnoreply@blogger.com0