Note that there are some explanatory texts on larger screens.

plurals
  1. PORetaining a locally-created NSMutableDictionary -- Necessary or not?
    text
    copied!<p>Hey all, I've got a few question on a segment of code here that I'd really appreciate some feedback on. Basically I'm creating a temporary NSMutableDictionary in order to pull some values out of a plist. I'll then run a check on one of those values to determine if I want to do anything else with the plist.</p> <p>Code is below, but my questions are as follows:</p> <p>1) Main questions </p> <p>Because I may be passing this locally-created Dictionary to another method, to be used there as source data, do I need to retain the dictionary here? If so, what is the proper method? </p> <p>I'm still learning the language, and in addition to not knowing whether a retain/release is even necessary here, the method I've implemented below doesn't seem to do anything. When I dump the dictionary to the console before and after release, the same values are printed out (though perhaps that would happen even if the release were successful, since nothing else has overwritten that memory segment yet?)</p> <p>2) Less important questions:</p> <p>Does "return" immediately jump us out of the entire "multitaskingResumeCheck" function, without executing any further code inside? Or does it just jump out of the "else" loop? I tried putting the "NSMutableDictionary *dict" dictionary creation inside of the filemanager if loop and I was getting an error from the "NSNumber *wrappedATZ..." line about dict being undeclared. What was my error here?</p> <p>Thanks in advance for all your help. I've put "//QUESTION TAG" on all pertinent lines.</p> <pre><code>- (void) multitaskingResumeCheck { NSLog(@"Running multitaskingResumeCheck"); // Get the path to the "Documents" directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get the path to our plist ("Documents/foo.plist") NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"lastStopwatchState.plist"]; // check if our plist already exists in the Documents directory NSMutableDictionary *dict; // QUESTION TAG (Why can't I do this in the "filemanager if" below) NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:plistPath]) { // If it does, read it. NSLog(@"dict existed, reading %@", plistPath); dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; [dict retain]; // QUESTION TAG (Necessary? If so, correct?) } else { // If not, we're done here. return; } // Now check whether the timers were zeroed on exit NSNumber *wrappedATZ = [dict valueForKey:@"allTimersZeroed"]; // QUESTION TAG (Got undeclared error if dict declaration was in wrong place (see above). Maybe I misunderstand "return"? BOOL timersWereZeroed = [wrappedATZ boolValue]; // If they were not Zeroed, then we'll load in our data if (!timersWereZeroed) { NSLog(@"Timers were not zeroed. Loading Data."); [self loadResumeData:dict]; } // Dump the contents of the dictionary to the console NSLog(@"dumping..."); for (id key in dict) { NSLog(@"key=%@, value=%@", key, [dict objectForKey:key]); } [dict release]; // QUESTION TAG (Necessary? If so, correct?) // Dump the contents of the dictionary to the console NSLog(@"dumping..."); for (id key in dict) { NSLog(@"key=%@, value=%@", key, [dict objectForKey:key]); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload