Further, in your code:
NSString *URLforAll = [[NSString alloc] init];
This never makes senses. Here are some issues:
- You are allocating an object, which you then overwrite indiscrimanently with "URLforAll = [dict objectForKey:@"URL"];". So you would need to release it before overwriting it.
- It is allocated (ie, at the end of the loop, you "own" it and will have to release it. But
[dict objectForKey:@"URL"] returns an object which you don't own. So at the end of the loop, you don't know whether you own URLforAll or not.
- And finally, [[NSString alloc] init] never makes sense because you should just use @"", which returns a constant, empty, NSString which is impervious to retain/release/autorelease issues.
Dealing also with the isEqualToString issue, but ignoring amrox's much better solution, the code would be:
NSString *path = [[NSBundle mainBundle] pathForResource:@"DataBase" ofType:@"plist"];
NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSString *URLforAll = @"";
for (id key in rootDict) {
if ( [key isEqualToString:rowString] ) {
NSDictionary *dict = [rootDict objectForKey:key];
URLforAll = [dict objectForKey:@"URL"];
}
}
[[URLforAll retain] autorelease];
[rootDict release];
Note that objectForKey may well return an internal reference to the object which will become invalid when you release the dictionary, hence the need for retaining the object if you want to keep it around longer than the life of the dictionary.
amrox's use of:
NSString *path = [[NSBundle mainBundle] pathForResource:@"DataBase" ofType:@"plist"];
NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSString *URLString = [[rootDict objectForKey:key] objectForKey:@"URL"];
[[URLString retain] autorelease];
[rootDict release];
if ( !URLString ) {
URLString = @"";
}
is a better solution, but you should undertand what is wrong with your original solution as well.