【问题标题】:RegexKitLite in IOS5IOS5 中的 RegexKitLite
【发布时间】:2011-12-09 23:43:58
【问题描述】:

我需要在我的应用程序中使用 RegexKitlite 作为字符串验证的一部分。 还添加了 libicucore.A.dylib 。 目前使用 xcode 4.2、Base sdk iOS 5.0、Apple LLVM 编译器 3.0、architechture armv7。 将 regexkit 文件夹添加到我的应用程序会导致太多错误,例如 自动参考计数错误, 将 Objective-C 指针类型 'NSString *' 转换为 C 指针类型 'CFStringRef' 等

请帮忙;我哪里出错了。

【问题讨论】:

    标签: objective-c regex xcode ios5 nsstring


    【解决方案1】:

    我用两种方法替换了 RegexKitLite。

    字符串结果:

    +(NSString*) regExString: (NSString *) pattern withString: (NSString *) searchedString {
        NSError  *error = nil;
        NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
        NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range:  NSMakeRange(0, [searchedString length])];
        if ([searchedString substringWithRange:[match rangeAtIndex:1]]) {
            return [searchedString substringWithRange:[match rangeAtIndex:1]];
        } else {
            return @"";
        }
    }
    

    结果数组:

    +(NSArray *) regExArray:(NSString *) pattern withString: (NSString *) searchedString {
        NSMutableArray *results = [[NSMutableArray alloc] init];
        NSError *error;
        NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
        NSArray* matches = [regex matchesInString:searchedString options:0 range: NSMakeRange(0, searchedString.length)];
    
        for (NSTextCheckingResult* match in matches) {
            NSMutableArray *result = [NSMutableArray array];
            NSRange matchRange = [match range];
            NSString *numString = [searchedString substringWithRange:matchRange];
            [result addObject:numString];
    
            for (int i=1;i < (int)match.numberOfRanges;i++) {
                NSRange range = [match rangeAtIndex:i];
                @try {
                    NSString *numString = [searchedString substringWithRange:range];
                    [result addObject:numString];
                }
                @catch (NSException *exception) {
                    [result addObject:[NSNull null]];
                }
            }
           [results addObject:result];
       }
       return results;
    }
    

    【讨论】:

      【解决方案2】:

      您也可以仅通过添加标志来禁用 RegexKitLite 的 ARC:

      选择项目 -> 您的目标 -> 在选项卡上的“构建阶段”并打开“编译源”并为“RegexKitLite.m”添加标志“-fno-objc-arc”。

      更新: 如果你得到:

      未定义的符号: “_uregex_reset”,引用自: RegexKitLite.o 中的 _rkl_splitArray RegexKitLite.o 中的 _rkl_replaceAll “_uregex_appendTail”,引用自:.......

      然后你需要在选项卡“Build Settings”->“Linking”->“Other Linker Flags”中添加“-licucore”

      【讨论】:

      • 在现代,你应该去 Link Binary with Libraries 并添加“libicucore.dylib”
      【解决方案3】:

      CBGraham 是对的。或者,您可以禁用自动引用计数(项目 > 构建设置 > 搜索“自动引用计数”)。

      您显然必须进行手动引用计数,但 RegexKitLite 现在应该构建...

      【讨论】:

        【解决方案4】:

        你没有做错任何事。 Regexkit 还没有更新到 iOS 5。 iOS 5 的最大变化是不再保留、发布或自动发布。每个内存都是自动的,就像 Java 一样。 (除了它发生在编译时而不是运行时。所以它在概念上类似于 Java。大多数情况下。)

        无论如何,您可以使用 NSRegularExpression,而不是等待 Regexkit 更新。使用 Apple 的东西也是面向未来的,因为他们会不断更新自己的东西。

        祝你好运!

        【讨论】:

        • 它不像 java ;) 像...完全不像
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多