How to create range in Swift?

前端 未结 10 1496
时光说笑
时光说笑 2020-11-28 01:37

In Objective-c we create range by using NSRange

NSRange range;

So how to create range in Swift?

相关标签:
10条回答
  • 2020-11-28 02:27

    I find it surprising that, even in Swift 4, there's still no simple native way to express a String range using Int. The only String methods that let you supply an Int as a way of obtaining a substring by range are prefix and suffix.

    It is useful to have on hand some conversion utilities, so that we can talk like NSRange when speaking to a String. Here's a utility that takes a location and length, just like NSRange, and returns a Range<String.Index>:

    func range(_ start:Int, _ length:Int) -> Range<String.Index> {
        let i = self.index(start >= 0 ? self.startIndex : self.endIndex,
            offsetBy: start)
        let j = self.index(i, offsetBy: length)
        return i..<j
    }
    

    For example, "hello".range(0,1)" is the Range<String.Index> embracing the first character of "hello". As a bonus, I've allowed negative locations: "hello".range(-1,1)" is the Range<String.Index> embracing the last character of "hello".

    It is useful also to convert a Range<String.Index> to an NSRange, for those moments when you have to talk to Cocoa (for example, in dealing with NSAttributedString attribute ranges). Swift 4 provides a native way to do that:

    let nsrange = NSRange(range, in:s) // where s is the string
    

    We can thus write another utility where we go directly from a String location and length to an NSRange:

    extension String {
        func nsRange(_ start:Int, _ length:Int) -> NSRange {
            return NSRange(self.range(start,length), in:self)
        }
    }
    
    0 讨论(0)
  • 2020-11-28 02:28

    Xcode 8 beta 2 • Swift 3

    let myString = "Hello World"
    let myRange = myString.startIndex..<myString.index(myString.startIndex, offsetBy: 5)
    let mySubString = myString.substring(with: myRange)   // Hello
    

    Xcode 7 • Swift 2.0

    let myString = "Hello World"
    let myRange = Range<String.Index>(start: myString.startIndex, end: myString.startIndex.advancedBy(5))
    
    let mySubString = myString.substringWithRange(myRange)   // Hello
    

    or simply

    let myString = "Hello World"
    let myRange = myString.startIndex..<myString.startIndex.advancedBy(5)
    let mySubString = myString.substringWithRange(myRange)   // Hello
    
    0 讨论(0)
  • 2020-11-28 02:29

    You can use like this

    let nsRange = NSRange(location: someInt, length: someInt)
    

    as in

    let myNSString = bigTOTPCode as NSString //12345678
    let firstDigit = myNSString.substringWithRange(NSRange(location: 0, length: 1)) //1
    let secondDigit = myNSString.substringWithRange(NSRange(location: 1, length: 1)) //2
    let thirdDigit = myNSString.substringWithRange(NSRange(location: 2, length: 4)) //3456
    
    0 讨论(0)
  • 2020-11-28 02:30

    (1..<10)

    returns...

    Range = 1..<10

    0 讨论(0)
提交回复
热议问题