Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print class name on Swift? [duplicate]

Tags:

In Swift, we can no longer use "self.class".

I want to print own class name for description method in subclass of NSObject.

How can I print it? Or I should make new method for getting class name?


I checked the following question, but I couldn't get the answer.

How do I print the type or class of a variable in Swift?

How do you find out the type of an object (in Swift)?

import UIKit  class PrintClassName: NSObject {     override init() {         super.init()         println("\(self.dynamicType)")         // Prints "(Metatype)"          println("\(object_getClassName(self))");         // Prints "0x7b680910 (Pointer)"          println("\(self.className)")         // Compile error!          println(PrintClassName.self)         // Prints "(Metatype)"     } } 

 

2014.08.25 postscript

I checked the question.

Swift class introspection & generics

But println(PrintClassName.self) prints "(Metatype)"

And I want to get class name without writing the class name on source code.

like image 736
satomikko94 Avatar asked Aug 24 '14 20:08

satomikko94


1 Answers

import Foundation  class PrintClassName : NSObject {     override init() {         super.init()         println(NSStringFromClass(self.dynamicType))     } } 
like image 181
Ivica M. Avatar answered Oct 30 '22 06:10

Ivica M.