Note that there are some explanatory texts on larger screens.

plurals
  1. PONSDictionary and NSString, several messages return a strange value
    text
    copied!<p>I am going crazy with a problem which I cannot understand at all! I have a NSDictionary (named 'headers') with NSString objects as values. In this dictionary are stored header fields obtained from a HTTP response.</p> <p>I need to send a message to this dictionary with this code</p> <pre><code>NSString* cacheControlString = [headers valueForKey:@"Cache-Control"] </code></pre> <p>this message is sent inside a method which is called three times... what I have is that the first and the second time the message returns a correct value. The third time SOMETIMES it does not return a correct value, and debugger says about cacheControlString</p> <pre><code>@&lt;variable is not a NSString&gt; </code></pre> <p>But WHY?!?! and why not always? That method does not modify the dictionary, because it cannot see the dictionary! In order to be sure that I have full control on that dictionary I copied the headers returned by the response in another dictionary created by me.</p> <p>The other strange thing is that if I write this code:</p> <pre><code>NSString* cacheControlString = [headers valueForKey:@"Cache-Control"]; cacheControlString = [headers valueForKey:@"Cache-Control"]; cacheControlString = [headers valueForKey:@"Cache-Control"]; cacheControlString = [headers valueForKey:@"Cache-Control"]; cacheControlString = [headers valueForKey:@"Cache-Control"]; cacheControlString = [headers valueForKey:@"Cache-Control"]; cacheControlString = [headers valueForKey:@"Cache-Control"]; </code></pre> <p>it always returns a correct value... Does someone had this kind of problem?! I am really going crazy!</p> <h2>Here is the full code:</h2> <pre><code>- (void) firstMethod { BOOL mustRevalidate = [self checkCacheControlElementForElement:@"must-revalidate" inHeaders:[response headers]]; BOOL publicResponse = [self checkCacheControlElementForElement:@"public" inHeaders:[response headers]]; BOOL privateResponse = [self checkCacheControlElementForElement:@"private" inHeaders:[response headers]]; } - (BOOL) checkCacheControlElementForElement:(NSString*)element inHeaders:(NSDictionary*)headers { NSString* cacheControlString = [headers valueForKey:@"Cache-Control"]; return [self checkCacheControlElementForElement:element inHeader:cacheControlString]; } - (BOOL) checkCacheControlElementForElement:(NSString*)element inHeader:(NSString*)headerString { if (!headerString) return NO; Header* header = [[Header alloc] initWithName:@"Cache-Control" andValue:headerString]; for (HeaderElement* headerElement in [header elements]) { if ([[headerElement name] isEqualToString:element]) { [header release]; return YES; } } [header release]; return NO; } </code></pre> <p>Header and HeaderElement are classes creates by me which help for working with headers, but that work just with the passed NSString objects. What checkCacheControlElementForElement:inHeader: method does is just to search if in a header there is a element.</p> <p>I have seen that, if I remove the second row to the method checkCacheControlElementForElement:inHeaders: letting it to return simply YES I have not the problem... but WHY? Header and HeaderElement classes does not work with the dictionary!</p> <p>As you can see nobody changes the 'headers' dictionary and you can be sure that values inside the dictionary are not autoreleased while I am working with it...</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