3

I am making an iPhone app and I am loading information from a server. I send NSURLRequest to the server and get back a NSString value. This is working fine and the value I am getting back is the correct one. The problem is that when I try to add the value for the variable to a NSMutableDictionary I have made to store the values, it doesn't work. When I debug and look at the values of the NSMutableDictionary in Xcode it says 0 key/value pairs right after the line where I add the values. This is what my code looks like:

    NSArray *varsToLoad = [fixedData objectForKey:@"varsToLoad"];
    NSError *error;
    for(NSString *var in varsToLoad){
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: someURL]];
        [request setHTTPMethod:@"POST"];
        NSMutableString *file = [NSMutableString stringWithString:@"file="];
        NSString *file1 = [file stringByAppendingString:var];
        NSString *file2 = [file1 stringByAppendingString:@".txt"];
        [request setHTTPBody:[file2 dataUsingEncoding:NSUTF8StringEncoding]];
        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
        NSString *value = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        [varsLoaded setObject:value forKey:var];
    }

varsLoaded is declared at the @implementation and is a NSMutableDictionary.

0

3 Answers 3

3

The problem may be that you haven't initialized your varsLoaded by saying self.varsLoaded = [NSMutableDictionary dictionary];

So you're adding objects to a non-existing dictionary, but you're not getting an exception because this is normal in objective-c :)

Sign up to request clarification or add additional context in comments.

4 Comments

That occurred to me too but if that were the case you wouldn't get 0 key/value pairs in the debugger, you'd get nil.
No, it shows the uninitialized dictionary as 0 key/value pairs in the debugger window. I tested it.
davsan, @jrturton : Should response be retained?
I think NSString's init method should be retaining it in its own implementation. Because otherwise it would be so confusing for the SDK users, like you should retain the NSData before using it for initing an NSString and you should not release that NSData before releasing the NSString, etc... So I think not retaining the response is OK.
0

You should really check the response before processing any further. Wrap it in something like this:

if (response == nil) {
    // Check for problems

}
else {
     NSString *value = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        [varsLoaded setObject:value forKey:var];
}

Comments

-1

Change the first line of your code from NSArray to NSMutableArray and see if you have better luck.

It doesn't matter that you declared your varsLoaded as a NSMutableDictionary somewhere else... in the local context of that code above, the compiler believes varsToLoad is a immutable array and probably isn't even compiling your setObject: forKey: line. You're not getting warnings in the build log or in the console while you're running??

1 Comment

According to the code varsToLoad and VarsLoaded are different objects. varsLoaded is probably an NSMutableDictionary. downvote wasn't from me btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.