If-Then 语句混乱,NSURL 结果无法正常工作

发布于 2024-11-08 11:35:21 字数 3513 浏览 0 评论 0原文

我想做的是采用 [self getYear] 方法,该方法是预定义的,并以年份(即 1995)的形式返回 NSString,然后采用该数字并将其与实际年份相匹配,然后设置到 URL 变量 theClassyear。当这个项目运行时,我将其作为 -viewWillAppear 中设置方法的一部分,当我拉出视图(这是在选项卡栏控制器仅供参考)时,我会收到 NSURL 错误 101 或错误 -999。 [self getYear] 方法从 NSUserDefaults 字符串中获取字符串,该字符串由控制器推送的不同视图中的 UIPicker 设置。就像我说的,当我第一次打开视图并且运行此方法时,我从第一个 NSLog 中获得了正确的结果,但随后它运行了我的 if 语句并最终使用 else 语句,该语句设置了我应该返回的 NSURL null(根据我的 NSLog)。后来在代码中我有另一个 NSLog 再次打印 [self getYear] 结果,这也给了我正确的数字。不知何故,我的“如果-那么”逻辑没有正确运行,我希望就我可能做错的事情提供建议。提前致谢!!! :)

-(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 URLWithString:@"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];

}

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 URLWithString:@"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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2024-11-15 11:35:21

关于我上面的评论的更多信息

将这些数据放入 plist 中,获取 plist(在本例中将是一个名为 dict 的 NSDictionary),然后返回 [NSURL URLWithString:[dict objectForKey:[self getYear]]];

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]]];

雾里花 2024-11-15 11:35:21

当您在 Objective-C 中的 NSString 等对象上使用 == 时,您不是在比较对象的,而是比较内存地址* 如果您有两个字符串,并且您想查看它们的内容是否相同,则必须如 Mahesh 提到的那样,使用 isEqualToString:,这样可以进行正确的比较。

您的代码中还有一些其他方面可以改进。首先,您泄漏了 NSURL。您首先创建一个

NSURL *theClassYear = [[NSURL alloc] init];

,然后忽略它并在 if 的一个分支中创建另一个:

theClassYear = [NSURL URLWithString:@"http://www.facebook.com/group.php?gid=35443264449"];
// This creates a new NSURL instance and leaks the old one.

要解决此问题,只需删除初始分配并将其设为声明:

NSURL * theClassYear;   // You will assign to this name later, in a branch

接下来,正如 Mahesh 还指出的那样,一旦您说 < code>return 在方法中,该行之后的任何内容都不会被执行,因此您的 [theClassYear release] 不会发生。在这种情况下,它实际上也是不必要的。** 从 URLWithString: 返回的对象是自动释放的,在这种情况下这正是您想要的。

最后,请务必采纳 Rexeisen 的建议,将所有这些字符串放入字典中。它将使您的代码更易于阅读且更易于维护。


*与 Python、Perl 或 PHP 等解释语言或 C++ 等具有运算符重载的编译语言不同。
**事实上,如果它正在运行,它会因过度释放而导致崩溃。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文