I have an iOS application which downloads and parses a Twitter JSON feed and then presents that feed in a UITableView. This all works fine but I have one question:
我有一个iOS应用程序,它下载并解析Twitter JSON提要,然后在UITableView中显示该提要。一切正常,但我有一个问题:
When the user taps a UITableView cell, the app will look into the array "tweets_links" and see if that particular tweet has an attached URL, if it does then the web view will appear.
当用户点击UITableView单元格时,应用程序将查看数组“tweets_links”并查看该特定推文是否有附加的URL,如果有,则会显示Web视图。
Because not all tweets have website URLs, I have added a simple try catch statement (like in C++) which can tell me if there is an exception when trying to access that part of the array.
因为并非所有的推文都有网站URL,所以我添加了一个简单的try catch语句(比如C ++),它可以告诉我在尝试访问数组的那一部分时是否存在异常。
My question is: is this is good or bad approach to doing this??
我的问题是:这是好的还是坏的做法?
Here is my code:
这是我的代码:
int storyIndex = indexPath.row;
int url_test = 1;
NSString *url;
@try {
url = [[tweets_links[storyIndex] valueForKey:@"url"] objectAtIndex:0];
}
@catch (NSException *problem) {
// There is NO URL to access for this Tweet. Therefore we get the out of bounds error.
// We will NOT take the user to the web browser page.
// Uncomment the line below if you wish to see the out of bounds exception.
// NSLog(@"%@", problem);
url_test = 0;
}
if (url_test == 1) {
WebBrowser *screen = [[WebBrowser alloc] initWithNibName:nil bundle:nil];
self.seconddata = screen;
seconddata.web_url = url;
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:screen animated:YES completion:nil];
}
else if (url_test == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Info" message:@"There is no URL attatched to this Tweet." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alertView show];
[tweetTableView deselectRowAtIndexPath:indexPath animated:YES];
}
Is there a much better way of trying to achieve what I am doing???
有没有更好的方法来尝试实现我正在做的事情?
Thanks, Dan.
5 个解决方案
#1
5
Using try and catch is Objective-C isn't encouraged there are other ways checking and handling errors
使用try和catch是不鼓励Objective-C有其他方法检查和处理错误
// firstObject will return the first object in the array or nil if the array is empty.
url = [[tweets_links[storyIndex][@"url"]] firstObject];
if (!url) {
// handle the case for no url
} else {
// do something with url
}
Since sending a message to nil
is safe in Objective-C and returns nil
it's safe to chain calls. e.g. If the dictionary didn't have an object for that key, then it would return nil
and sending firstObject
to nil
returns nil
.
由于在Objective-C中向nil发送消息是安全的并且返回nil,因此对链接调用是安全的。例如如果字典没有该键的对象,那么它将返回nil并将firstObject发送到nil返回nil。
#2
1
Using either if the below approaches will be fine because TRY CATCH is used to catch programming errors and use objectForKey: instead of valueForKey:
使用以下两种方法都可以,因为TRY CATCH用于捕获编程错误并使用objectForKey:而不是valueForKey:
if ([tweets_links[storyIndex] objectForKey:@"url"] != nil)
OR
if ([url isKindOfClass:[NSString class]])
{
// Code handling the URL
}
else
{
// Code handling there is no URL
}
#3
0
I don't know a ton about the Twitter feed, but you can probably check for a nil
value returned from objectForKey:
like so
我不太了解Twitter提要,但你可以检查objectForKey返回的nil值:就像这样
if ([tweets_links[storyIndex] objectForKey:@"url"] != nil) { /* process the URL */ }
Your code assumes that the value is always an array of at least size = 1, it would be safer to inspect the @"url" key's value before assuming it's an array.
您的代码假定该值始终是至少size = 1的数组,在假定它是一个数组之前检查@“url”键的值会更安全。
#4
0
Using exceptions in Objective-C is throwned upon. Exceptions are reserved for programming errors. You don't catch them, you fix the code. With a JSON document, you never have any guarantees what you received, so just be careful.
在Objective-C中使用异常是有道理的。例外保留用于编程错误。您没有捕获它们,您修复了代码。使用JSON文档,您永远不会保证收到的内容,所以请小心。
NSString* url = nil;
NSArray* linksArray = nil;
NSDictionary* linkDict = nil;
NSArray* urlArray = nil;
if ([tweet_links isKindOfClass:[NSArray class]])
linksArray = tweet_links;
if (storyIndex >= 0 && storyIndex < linksArray.count)
linkDict = linksArray [storyIndex];
urlArray = linkDict [@"url"];
if ([urlArray isKindOfClass:[NSArray class]] && urlArray.count > 0)
url = urlArray [0];
if ([url isKindOfClass:[NSString class]])
{
// Code handling the URL
}
else
{
// Code handling there is no URL
}
Note that sending messages to a nil object always returns 0 / NO / nil as appropriate.
请注意,将消息发送到nil对象始终返回0 / NO / nil。
And please get into the habit of naming variables properly. You wrote "int url_test = 1;". What does url_test mean? I read the variable name, I have no idea what it means. I need to understand all the code. Making it "int" means it could be 0, 1, 2, 20000 or whatever. If you write instead "BOOL urlValid = YES;" that is clear: It means that you have a valid URL.
请养成正确命名变量的习惯。你写了“int url_test = 1;”。 url_test是什么意思?我读了变量名,我不知道它意味着什么。我需要了解所有代码。使它成为“int”意味着它可以是0,1,2,200或其他。如果你改写“BOOL urlValid = YES;”很明显:这意味着你有一个有效的URL。
#5
0
Since url value is a NSString value, you could use length
to check both if it's nil and if not, if it has any value (not empty string). You can check then if this NSString is a valid url.
由于url值是一个NSString值,你可以使用length来检查它是否为零,如果没有,是否有任何值(不是空字符串)。您可以检查此NSString是否是有效的URL。
- (BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
....
NSString *url = [[tweets_links[storyIndex][@"url"]] firstObject];
if ([url length] && [self validateUrl: url]) {
// Has a valid URL
}
#1
5
Using try and catch is Objective-C isn't encouraged there are other ways checking and handling errors
使用try和catch是不鼓励Objective-C有其他方法检查和处理错误
// firstObject will return the first object in the array or nil if the array is empty.
url = [[tweets_links[storyIndex][@"url"]] firstObject];
if (!url) {
// handle the case for no url
} else {
// do something with url
}
Since sending a message to nil
is safe in Objective-C and returns nil
it's safe to chain calls. e.g. If the dictionary didn't have an object for that key, then it would return nil
and sending firstObject
to nil
returns nil
.
由于在Objective-C中向nil发送消息是安全的并且返回nil,因此对链接调用是安全的。例如如果字典没有该键的对象,那么它将返回nil并将firstObject发送到nil返回nil。
#2
1
Using either if the below approaches will be fine because TRY CATCH is used to catch programming errors and use objectForKey: instead of valueForKey:
使用以下两种方法都可以,因为TRY CATCH用于捕获编程错误并使用objectForKey:而不是valueForKey:
if ([tweets_links[storyIndex] objectForKey:@"url"] != nil)
OR
if ([url isKindOfClass:[NSString class]])
{
// Code handling the URL
}
else
{
// Code handling there is no URL
}
#3
0
I don't know a ton about the Twitter feed, but you can probably check for a nil
value returned from objectForKey:
like so
我不太了解Twitter提要,但你可以检查objectForKey返回的nil值:就像这样
if ([tweets_links[storyIndex] objectForKey:@"url"] != nil) { /* process the URL */ }
Your code assumes that the value is always an array of at least size = 1, it would be safer to inspect the @"url" key's value before assuming it's an array.
您的代码假定该值始终是至少size = 1的数组,在假定它是一个数组之前检查@“url”键的值会更安全。
#4
0
Using exceptions in Objective-C is throwned upon. Exceptions are reserved for programming errors. You don't catch them, you fix the code. With a JSON document, you never have any guarantees what you received, so just be careful.
在Objective-C中使用异常是有道理的。例外保留用于编程错误。您没有捕获它们,您修复了代码。使用JSON文档,您永远不会保证收到的内容,所以请小心。
NSString* url = nil;
NSArray* linksArray = nil;
NSDictionary* linkDict = nil;
NSArray* urlArray = nil;
if ([tweet_links isKindOfClass:[NSArray class]])
linksArray = tweet_links;
if (storyIndex >= 0 && storyIndex < linksArray.count)
linkDict = linksArray [storyIndex];
urlArray = linkDict [@"url"];
if ([urlArray isKindOfClass:[NSArray class]] && urlArray.count > 0)
url = urlArray [0];
if ([url isKindOfClass:[NSString class]])
{
// Code handling the URL
}
else
{
// Code handling there is no URL
}
Note that sending messages to a nil object always returns 0 / NO / nil as appropriate.
请注意,将消息发送到nil对象始终返回0 / NO / nil。
And please get into the habit of naming variables properly. You wrote "int url_test = 1;". What does url_test mean? I read the variable name, I have no idea what it means. I need to understand all the code. Making it "int" means it could be 0, 1, 2, 20000 or whatever. If you write instead "BOOL urlValid = YES;" that is clear: It means that you have a valid URL.
请养成正确命名变量的习惯。你写了“int url_test = 1;”。 url_test是什么意思?我读了变量名,我不知道它意味着什么。我需要了解所有代码。使它成为“int”意味着它可以是0,1,2,200或其他。如果你改写“BOOL urlValid = YES;”很明显:这意味着你有一个有效的URL。
#5
0
Since url value is a NSString value, you could use length
to check both if it's nil and if not, if it has any value (not empty string). You can check then if this NSString is a valid url.
由于url值是一个NSString值,你可以使用length来检查它是否为零,如果没有,是否有任何值(不是空字符串)。您可以检查此NSString是否是有效的URL。
- (BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
....
NSString *url = [[tweets_links[storyIndex][@"url"]] firstObject];
if ([url length] && [self validateUrl: url]) {
// Has a valid URL
}