将下标函数从 Objective C 转换为 Swift

Convert subscript function from Objective C to Swift

我正在尝试将一些 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;
}

我在下标部分卡住了,因为我似乎无法为 Indexes 分配整数值,而且从 Index 到 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
//                        }
//                }

            }
        }
    }

更新:下面的解决方案。

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

The Github repo

使用发布的 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{[=10=]}
}