Alexander Tsepkov Alexander Tsepkov - 4 months ago 10x
Swift Question

NSRange from first occurrence until end of string

I'm pulling my hair out trying to generate a valid NSRange, it doesn't seem like it should be this complicated so I'm guessing I'm using the wrong approach. Here is what I'm trying to do:

I have a string with some unicode character in it:

"The quick brown fox\n❄jumped\n❄over the lazy dog"

I want to create an NSRange from that character until the end of string, and while I can get the corresponding index for the first occurrence of the character:


I can't seem to get the end of the string in a consistent format (something that I can pass to
) to actually generate the range. This seems like it should be pretty simple, yet I've been stuck for over an hour now trying to figure out how to get it to work, I keep ending up with
types that I can't cast to integers to convert back to length that NSMakeRange requires for its second element.

Ideally I'd do something like this (which is invalid due to incompatible and non-castable types (Index vs Int)):

let start = text.rangeOfString("❄")?.startIndex
NSMakeRange(start, text.endIndex - start)

I am using Swift, so I have the ability to use Swift's
, if it will make things easier, although it seems to be yet another range representation different from NSRange and I'm not sure how compatible the two are (don't want to run into another dimension of


Cast your String as NSString.

You will be able to use Foundation's .rangeOfString instead of Swift's .rangeOfString.

The Foundation's one will return an NSRange.

Be careful though, it doesn't work the same as Swift's method with Unicode, and NSRange and Range are not compatible (although there's ways to convert them).