Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your questions are more or less completely answered in the <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH" rel="nofollow">Cocoa Memory Management Rules</a> although it may not be completely clear exactly how.</p> <p>Firstly, you should refactor your code so that the brace structure matches the actual control flow. Namely, everything after the </p> <pre><code>else { return; } </code></pre> <p>Should be inside the if part so it look something like this:</p> <pre><code>- (void) multitaskingResumeCheck { // Some stuff not relevant to the question NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:plistPath]) { // If it does, read it. NSLog(@"dict existed, reading %@", plistPath); NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; [dict retain]; // QUESTION TAG (Necessary? If so, correct?) // Some stuff not relevant to the question 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?) } </code></pre> <p>I've moved everything after the else part into the if part because it is effectively exactly the same. Since <code>dict</code> is now no longer used except in the if part, I've moved its declaration there. I've also deleted the second dump because dict no longer exists in the same scope. Other than that, this is functionally identical to what you had.</p> <p>One of the points of the memory management rules says:</p> <blockquote> <p>A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. </p> </blockquote> <p>This answers your first question. The dictionary you received from <code>+dictionaryWithContentsOfFile:</code> will remain valid within the method. That includes any methods that this method calls, passing dict as a parameter. So, in this instance, the retain and release are not necessary. The reason the second dump worked before is because the retain and release pair had no net effect on the ownership of the dictionary.</p> <p>In answer to your second question, the <code>return</code> exits the function immediately without executing any code after it. This is why I was able to refactor the function in the way I did. </p> <p>The reason you had an error when you moved the declaration of dict was that, in your code, dict was being referred to outside of the block (statements enclosed in <code>{ ... }</code> are called a block) in which it was declared. C scoping rules prevent this.</p> <p>As a final aside, to log the contents of a dictionary, you can do:</p> <pre><code> NSLog(@"%@", dictionary); </code></pre> <p>No need to loop through the keys.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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