Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Current Module Name in Swift

Tags:

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?

like image 212
mohamede1945 Avatar asked Sep 18 '15 20:09

mohamede1945


2 Answers

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

like image 194
Max Chuquimia Avatar answered Nov 06 '22 01:11

Max Chuquimia


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]) }
}
like image 39
aryaxt Avatar answered Nov 06 '22 01:11

aryaxt