Music_Player3/MusicPlayer/MP/Common/Extension(扩展)/String.swift
2024-05-14 15:04:59 +08:00

31 lines
1.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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)
}
}