检查 NSArray 中是否包含 NSString hasPrefix [英] Check if NSString hasPrefix that is contained in NSArray
本文介绍了检查 NSArray 中是否包含 NSString hasPrefix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 if
语句来检查 URL 是否以前缀开头:
I have this if
statement to check if a URL starts with a prefix:
NSString *baseUrl = @"http://example.com/";
NSString *currentURL = @"http://example.com/confirm";
if( [currentURL hasPrefix:[baseUrl stringByAppendingString:@"confirm"]] ){
// True, so do something...
}
我想修改我的 if
语句的 hasPrefix
部分,以便在 currentUrl
附加 ANY 时返回 true 此处显示的 NSArray
中的值:
I would like to modify the hasPrefix
part of my if
statement so that it returns true if currentUrl
is appended by ANY of the values in my NSArray
shown here:
NSArray *pages = [NSArray arrayWithObjects:@"confirm",@"products",nil];
我该怎么做?
推荐答案
只要遍历你的 pages 数组并设置一个布尔标志(如果存在任何术语)
just iterate through your pages array and set a boolean flag if any of the terms exist
//
// customstring.m
//
// Created by rbertsch8 on 8/2/13.
//
//
#import "customstring.h"
@implementation customstring
-(bool)hasPrefixArray:(NSArray*)array withbaseURL:(NSString*)baseUrl
{
bool hasprefix = NO;
for(int i = 0; i < [array count] || hasprefix == NO; i++)
{
hasprefix = [self hasPrefix:[baseUrl stringByAppendingString:[array objectAtIndex:i]]];
}
return hasprefix;
}
@end
现在在您的代码中执行以下操作://导入自定义字符串文件#import NSStringPrefix.h
Now in your code do the following: //import your custom string file #import NSStringPrefix.h
NSStringPrefix *customURL = yoururl.com
NSString *baseUrl = baseurl.com
NSArray *yourarray = [[NSArray alloc] initWithObjects: @"one", @"two"]
if([customURL hasPrefixArray:yourarray withbaseURL:baseUrl])
{
//dowork
}
更新
在全局类中定义变量.
#define companyOrSiteName @"Company X"
#define baseUrl @"http://www.example.com/" // Set base URL for site.
#define customUrlScheme @"example://" // Set custom URL Scheme for app.
#define openInModal [NSArray arrayWithObjects: @"contact-us.php",@"products/",nil]
#define openInSafari [NSArray arrayWithObjects: @"about",@"products/resources/download",nil]
#define twitterHandle @"Example" // Twitter username without the "@" symbol
#define kTrackingId @"UA-4564564-3" // Set Google Analytics iOS App Tracking code.
这篇关于检查 NSArray 中是否包含 NSString hasPrefix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文