//
//  String.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/4/7.
//

import Foundation
extension String {
    /// 字符串转类type
    func classFromString() -> AnyClass? {
        // 获swift中的命名空间名
        var name = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String
        // 如果包名中有'-'横线这样的字符，在拿到包名后，还需要把包名的'-'转换成'_'下横线
        name = name?.replacingOccurrences(of: "-", with: "_")
        // 拼接命名空间和类名，”包名.类名“
        let fullClassName = name! + "." + self
        // 因为NSClassFromString()返回的AnyClass?，需要给个默认值返回!
        let anyClass: AnyClass? = NSClassFromString(fullClassName)
        // 类type
        return anyClass
    }
}
extension Range where Bound == String.Index {
    func toNSRange(in string: String) -> NSRange {
        let utf16IndexStart = string.utf16.distance(from: string.utf16.startIndex, to: lowerBound)
        let utf16IndexEnd = string.utf16.distance(from: string.utf16.startIndex, to: upperBound)
        return NSRange(location: utf16IndexStart, length: utf16IndexEnd - utf16IndexStart)
    }
}
