Skip to content

Instantly share code, notes, and snippets.

@artemkrachulov
Last active July 23, 2016 10:01
Show Gist options
  • Save artemkrachulov/3d21d6ca2a437d8a52e8c973e5cdefb4 to your computer and use it in GitHub Desktop.
Save artemkrachulov/3d21d6ca2a437d8a52e8c973e5cdefb4 to your computer and use it in GitHub Desktop.
Convert to NSRange object.
//
// Range+toNSRange.swift
//
// Created by Artem Krachulov.
// Copyright (c) 2016 Artem Krachulov. All rights reserved.
// Website: http://www.artemkrachulov.com/
//
import UIKit
extension Range {
/// Convert to NSRange object.
///
/// Usage:
///
/// let range = 10..<15 // 10..<15
/// let convertedRange = range.toNSRange() // (10,5)
public func toNSRange() -> NSRange {
let loc = startIndex as! Int
let len = (endIndex as! Int) - loc
return NSMakeRange(loc, len)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment