0

我对 iOS 比较陌生,在检查字符串对象是否包含 URL 或字符串时遇到问题?

NSMutableArray *Arr=[NSMutableArray alloc]initWithObject:@"Welcome", @"http://abcd.com/Images/bus.png", nil];

int i;

i++;

NSString *str=[Arr objectAtIndex:i];

现在,我想检查条件,如果字符串包含“欢迎”,必须显示在标签上,或者如果它是 URL,我需要在 ImageView 中显示该 URL 图像。那么我该如何检查呢?请帮我解决这个问题。

4

4 回答 4

1

不要将两者都作为 NSString 启动,而是尝试通过将 url 设为 NSURL(专门用于 url 的特殊容器)来区分它们:

NSMutableArray* Arr = [NSMutableArray alloc]initWithObject:@"Welcome", [NSURL URLWithString:@"http://abcd.com/Images/bus.png"], nil];

for(id object in Arr)
{
    if([object isKindOfClass:[NSString class]])
    {
        NSString* string = object;
        NSLog(@"String: %@", string);
    }
    else if([object isKindOfClass:[NSURL class]])
    {
        NSURL* url = object;
        NSLog(@"URL: %@", url);
    }
}
于 2013-10-11T12:48:15.810 回答
1

像这样试试

NSMutableArray *Arr=[[NSMutableArray alloc]initWithObjects:@"Welcome", @"http://abcd.com/Images/bus.png",nil];

    NSString *st=nil;
    for(NSString *string in Arr)
    {
     NSArray *matches = [detector 
                 matchesInString:string
                                    options:0
                                      range:NSMakeRange(0,     
                                     [string length])];
   for (NSTextCheckingResult *match in 
          matches) {
          if ([match resultType] ==    
         NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
         } else 
       {
        NSlog(@"it is a string");
       }
    }

}

于 2013-10-11T13:21:35.110 回答
0

要检查NSString是否包含 URL,您可以尝试此代码

if ([stringName hasPrefix:@"http://"] || [stringName hasPrefix:@"https://"]) {
    //show imageVivew
} 
于 2013-10-11T12:34:22.620 回答
0

试试这个,它会帮助你:

NSMutableArray *Arr=[[NSMutableArray alloc]initWithObjects:@"Welcome", @"http://abcd.com/Images/bus.png", nil];

if([Arr count])

{

for (NSString *str in Arr)

    {
        if([str isEqualToString:@"Welcome"])

        {
            NSLog(@"str is %@",str);

            //do whatever you want
        }

        if([str isEqualToString:@"http://abcd.com/Images/bus.png"])

        {
            NSLog(@"str is %@",str);

            //do whatever you want
        }
    }
}
于 2013-10-11T12:37:49.630 回答