Thursday 15 July 2010

ios - Calling NSStringFromClass on a Swift Class in Objective-C returns module mangled name -



ios - Calling NSStringFromClass on a Swift Class in Objective-C returns module mangled name -

i aware of this question regarding how can readable class name of objective-c class in swift.

what want accomplish getting readable class name of swift class within objective-c without mangling class name module.

so if have swift class:

class foo: nsobject{}

then within objective-c love utilize convenient nsstringfromclass convert class name string.

i expect nsstringfromclass([foo class]) homecoming @"foo" instead returns @"bar.foo" withbarbeing module name.

i came across gist seems little hacky , messy, there improve way? doesn't include typing class name manually string preferred.

swift 2.1

with swift 2.1 seems if sufficient

class yourclassname: nsobject { }

and use:

var str = string(yourclassname)

i have not tested objective-c code though

before swift 2.1:

just set @objc(yourclassname) in swift class:

@objc(yourclassname) class yourclassname: nsobject { }

and can utilize nsstringfromclass this:

nsstringfromclass(yourclassname.self)

it should work objective-c then.

ios objective-c swift xcode6

No comments:

Post a Comment