【问题标题】:Convert subscript function from Objective C to Swift将下标函数从 Objective C 转换为 Swift
【发布时间】:2020-07-01 01:49:53
【问题描述】:

我正在尝试将一些 Objective C 代码转换为 Swift,但无法通过下标正确完成。 这是我试图迁移到 Swift 的方法:

- (NSArray *)rangesOfSubstringAlphaNumeric:(NSString *)substring rangesLimit:(NSUInteger)rangesLimit {
    NSAssert(rangesLimit, @"A range limit grather than 0 must be specified");
    if (!substring.length) {
        return nil;
    }
    static NSCharacterSet * restrictedCharacters = nil;
    if (!restrictedCharacters) {
        restrictedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    }
    NSArray * substrings = [substring componentsSeparatedByCharactersInSet:restrictedCharacters];
    NSMutableArray * allRanges = [NSMutableArray array];
    NSString *searchedString = self;
    for (NSString *stringToMatch in substrings) {
        if (![stringToMatch isEqualToString:@""]) {
            NSRange aRange;
            NSUInteger lastLocation = 0;
            NSUInteger foundRanges = 0;
            while (foundRanges++ < rangesLimit &&
                   (aRange = [searchedString localizedStandardRangeOfString:stringToMatch]).location != NSNotFound) {
                searchedString = [searchedString substringFromIndex:aRange.location + aRange.length];
                aRange.location = aRange.location + lastLocation;
                lastLocation = aRange.location + aRange.length;
                [allRanges addObject:[NSValue valueWithRange:aRange]];
            }
        }
    }
    return allRanges.count ? [allRanges copy] : nil;
}

我卡在下标部分,因为我似乎无法为索引分配整数值,并且从索引到 Int 的转换对我来说是失控的,我有点卡住了,这就是我设法做到的:

func rangesOfAlphanumeric(substring: String, limit: UInt) -> [Range<String.Index>] {
        guard limit > 0, !substring.isEmpty else {
            if limit == 0 {
                assert(false, "limit must be greather than 0")
            }
            return []
        }
        var searchedString = self
        let substrings = substring.components(separatedBy: NSCharacterSet.restricted)
        for stringToMatch in substrings {
            if !stringToMatch.isEmpty {

//                var aRange: Range<String.Index>?
//                var lastLocation: UInt = 0
//                var foundRanges: UInt = 0

//                while foundRanges < limit,
//                    let tempRange = searchedString.localizedStandardRange(of: stringToMatch),
//                    !tempRange.isEmpty {
//
//                        searchedString = String(searchedString[tempRange.upperBound...])
//                        if let lastLocation = lastLocation {
//                            aRange = temp
//                        }
//                }

            }
        }
    }

更新:下面的解决方案。

【问题讨论】:

  • @matt 如果我想使用 NSRange 我会使用它,当你刚刚说我想将 Objective C 转换为 Swift 时,我的问题有什么不清楚的地方?并使用下标而不是 NSRange?

标签: objective-c swift substring subscript


【解决方案1】:

设法使用here 发布的ranges 函数解决了问题:

func rangesOfAlphanumeric(substring: String) -> [Range<String.Index>] {
    var searchedString = self
    let substrings = substring.components(separatedBy: NSCharacterSet.restricted)
    return substrings.compactMap { (stringToMatch) -> [Range<String.Index>]? in
        guard !stringToMatch.isEmpty else {
            return nil
        }
        let ranges = searchedString.ranges(of: stringToMatch, options: [
            .diacriticInsensitive,
            .caseInsensitive
        ])

        if let lastRange = ranges.last {
            searchedString = String(searchedString[index(after: lastRange.upperBound)])
        }
        return ranges
    }.flatMap{$0}
}

【讨论】:

【解决方案2】:

我用 swift 5 创建了这个 repo 非常好用 一切都已经设置好了。您只需更改 IAP ids

The Github repo

【讨论】:

  • 请不要发布指向外部存储库的链接,尤其是那些不回答问题的链接。
  • 这是某种奇怪的垃圾邮件吗?
  • @matt 不是垃圾邮件 afaikt,只是一个奇怪的离题答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 2016-01-24
  • 1970-01-01
相关资源
最近更新 更多