What I would like to do is take the [self getYear] method which is predefined and returns an NSString in the form of a year (i.e. 1995), and then take that number and match it to which year it actually is, and set that to a URL variable, theClassyear. When this project is run, i have this as part of a setup method in -viewWillAppear and when I pull up the view (this is in a tab bar controller fyi) then I get either an NSURL error 101 or error -999. The [self getYear] method takes it's string from a NSUserDefaults string that is set by a UIPicker in a different view pushed by the controller. Like I said, when I first open the view and this method runs I get the correct result from the first NSLog, but then it runs through my if statements and ends up using the else statement which sets my NSURL that is supposed to be returned to null (according to my NSLog). Later on in the code I have another NSLog that prints the [self getYear] result again and that gives me the right number also. somehow my if-then logic is not running through correctly and I would love advice on what I may be doing wrong. Thanks in advance!!! :)
-(NSURL *)theClassYear{
NSURL *theClassYear = [[NSURL alloc] init];
NSLog(@"the user default loaded for the year is: %@",[self getYear]);
if ([self getYear] == @"1995") {
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=35443264449"];
}
else if ([self getYear] == @"1996"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=50371222704"];
}
else if ([self getYear] == @"1997"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=101880069858690"];
}
else if ([self getYear] == @"1998"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=98761155252"];
}
else if ([self getYear] == @"1999"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=34955119113"];
}
else if ([self getYear] == @"2000"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=41438241821"];
}
else if ([self getYear] == @"2001"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=44108047608"];
}
else if ([self getYear] == @"2002"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=98700780436"];
}
else if ([self getYear] == @"2003"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=36811255052"];
}
else if ([self getYear] == @"2004"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=40331531709"];
}
else if ([self getYear] == @"2005"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=97724430117"];
}
else if ([self getYear] == @"2006"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=50868469971"];
}
else if ([self getYear] == @"2007"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=38506528654"];
}
else if ([self getYear] == @"2008"){
theClassYear = [NSURL URLWithStrin开发者_高级运维g:@"http://www.facebook.com/group.php?gid=55466866222"];
}
else if ([self getYear] == @"2009"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=105187085612"];
}
else if ([self getYear] == @"2010"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=39303927757"];
}
else if ([self getYear] == @"2011"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=281837582821"];
}
else if ([self getYear] == @"2012"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=79162636609"];
}
else if ([self getYear] == @"2013"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/home.php?sk=group_161338720557447"];
}
else if ([self getYear] == @"2014"){
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/home.php?sk=group_125352334187406"];
}
else {
NSLog(@"no matches");
}
NSLog(@"the url for the year you chose is: %@",theClassYear);
return theClassYear;
[theClassYear release];
}
More on my comment above
Put this data in a plist, grab the plist (which will be an NSDictionary called dict in this case) and then return [NSURL URLWithString:[dict objectForKey:[self getYear]]];
When you use ==
on objects such as NSString
in Objective-C, you are not comparing the values of the objects, but the memory address of the object.* If you have two strings, and you want to see whether their contents are the same, you must, as Mahesh mentioned, use isEqualToString:
, which does the correct comparison.
There are a couple of other things that can improved in your code. First, you are leaking an NSURL
. You first create one with
NSURL *theClassYear = [[NSURL alloc] init];
and then you ignore it and create another one in one of the branches of your if:
theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=35443264449"];
// This creates a new NSURL instance and leaks the old one.
To fix this, simply remove the initial assignment and make it a declaration:
NSURL * theClassYear; // You will assign to this name later, in a branch
Next, as Mahesh also pointed out, once you say return
in a method, nothing past that line gets executed, so your [theClassYear release]
does not occur. In this case, it's actually unnecessary, too.** The object that you get back from URLWithString:
is autoreleased, which in this case is exactly what you want.
Finally, please do take Rexeisen's advice about putting all those strings into a dictionary. It will make your code much easier to read and more maintainable.
*Unlike interpreted languages such as Python, Perl, or PHP, or compiled languages with operator overloading such as C++. **In fact, if it were running it would cause a crash due to overreleasing.
精彩评论