Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with adding objects to NSMutableDictionary

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.

like image 942
Vedaant Arora Avatar asked Aug 20 '26 04:08

Vedaant Arora


1 Answers

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 :)

like image 131
aslisabanci Avatar answered Aug 22 '26 12:08

aslisabanci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!