简体   繁体   中英

Print Current Module Name in Swift

How can I print the module name for a class in swift?

self.dynamicType prints only the class name without the module. How can I print the module?

For the not-so-pretty but currently working way, try:

let object = NSStringFromClass(MyClass) as NSString
let module = object.componentsSeparatedByString(".").first!

This doesn't seem to work for Foundation/UIKit etc objects though

Here is a different solution

// "To parse a #fileID expression, read the module name as the text before the first slash (/) and the filename as the text after the last slash"
func moduleName(fileId: String = #fileID) -> String? {
   fileId.firstIndex(of: "/").flatMap { String(fileId[fileId.startIndex ..< $0]) }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM