How to create range in Swift?

前端 未结 10 1500
时光说笑
时光说笑 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:06

    I created the following extension:

    extension String {
        func substring(from from:Int, to:Int) -> String? {
            if from=0 && to

    example of use:

    print("abcde".substring(from: 1, to: 10)) //nil
    print("abcde".substring(from: 2, to: 4))  //Optional("cd")
    print("abcde".substring(from: 1, to: 0))  //nil
    print("abcde".substring(from: 1, to: 1))  //nil
    print("abcde".substring(from: -1, to: 1)) //nil
    

提交回复
热议问题