【问题标题】:Check if NSString hasPrefix that is contained in NSArray检查 NSString hasPrefix 是否包含在 NSArray 中
【发布时间】:2013-08-02 14:38:38
【问题描述】:

我有这个 if 语句来检查 URL 是否以前缀开头:

NSString *baseUrl = @"http://example.com/";

NSString *currentURL = @"http://example.com/confirm";

if( [currentURL hasPrefix:[baseUrl stringByAppendingString:@"confirm"]] ){

    // True, so do something...

}

我想修改我的if 语句的hasPrefix 部分,以便如果currentUrl 附加了我的NSArray 中的ANY 值,则它返回true,如下所示:

NSArray *pages = [NSArray arrayWithObjects:@"confirm",@"products",nil];

我该怎么做?

【问题讨论】:

    标签: objective-c nsstring nsarray prefix


    【解决方案1】:

    只需遍历您的 pages 数组并设置一个布尔标志(如果存在任何术语)

    //
    //  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

    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.
    

    【讨论】:

    • 我正在寻找一种可以内联放置的解决方案,因为这需要在不同的文件中多次使用。有没有办法只修改 hasPrefix
    • 在这种情况下,您需要创建一个自定义 NSString 类并覆盖 hasprefix 方法(或创建一个新方法,如 hasPrefixArray),因此创建一个新文件 -> 目标 c 类 -> NSString 的子类'将在此类中将其命名为 NSStringPrefix 创建一个方法: -(bool)hasPrefixArray:(NSArray*)array 然后您只需将 #import NSStringPrefix.h 添加到您的代码中,您的 currentURL 变量现在将是 NSStringPrefix *currentURL
    • 我了解创建文件,但是您可以输入一段代码并通过示例进行演示吗?谢谢!
    • 我在上面编辑了我的代码,以展示如何在自定义 NSStringPrefix 类中实现新的 hasprefixarray 方法
    • 那我该怎么用呢?
    【解决方案2】:
    NSArray *parts = [currentUrl componentsSeparatedByString:@"/"];
    NSString *name = [parts objectAtIndex:1];
    

    在此之后,您可以检查字符串名称是否是 pages 数组中的元素。

    【讨论】:

    • 我不确定你的意思 - 我如何检查字符串名称是否是 pages 数组中的元素?这就是问题所在,而您的答案并未表明这一点。
    • 我的意思是遍历数组中的元素并将它们与字符串名称进行比较。或者,您可以使用 NSDictionary 来存储数组的元素以获得 O(1) 检索。
    【解决方案3】:

    对于那些想要避免腕管综合症的人:

    NSString *baseUrl = @"http://example.com/";
    NSString *currentURL = @"http://example.com/confirm";
    NSArray *pages = [NSArray arrayWithObjects:@"confirm",@"products",nil];
    BOOL hasSuffix = NO;
    [pages enumerateObjectsUsingBlock:^(NSString* page, NSUInteger idx, BOOL *stop) {
      if([currentURL hasPrefix: baseUrl] && [currentURL hasSuffix: page]){
        hasSuffix = YES;
        stop = YES;
      }
    }];
    

    你的描述听起来更像是在寻找一个后缀,所以你有它。

    【讨论】:

      【解决方案4】:

      很简单,使用下面的代码

      BOOL hasPrefixInArray = NO;
      
      for(int i = 0; i < [array count] && hasPrefixInArray == NO; i++)
      {
          hasPrefixInArray = [yourString hasPrefix:array[i]];
      }
      
      // check value here for hasPrefixInArray
      NSLog(@"%d",hasPrefixInArray);
      

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 2011-06-09
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        相关资源
        最近更新 更多