0

I have an issue where in Swift I have a class defined named Person, and when talking to some objective-c code, it will try to create new instances of that class.

Obviously NSClassFromString(@"Person") fails, because it needs to be a fully qualified name such as DBAccess_Test.Person.

My question is, am I able to programatically generate the DBAccess_Test. portion, by possibly searching through a registry of classes perhaps?

The reason being, that for an ORM we maintain, a Swift programmer may well decide to have all of his storage classes within a separate namespace to the application. But we wish to maintain standard table naming practices that match the names of the class as understood by the developer.

For instance, instead of a query being:

NSArray* r = [[[[Person query] 
             where:@"surname IN (SELECT surname FROM Employees)"]
             orderBy:@"forename"]
             fetch];

it would become:

NSArray* r = [[[[Person query] 
             where:@"surname IN (SELECT surname FROM DBAccess_Test_Employees)"]
             orderBy:@"forename"]
             fetch];

The ORM already has a bit of an identity crisis. We just wish to keep in check the table names, with the class names that the programmer will use.

Thanks in advance foe your help.

Adrian_H
  • 1,548
  • 1
  • 14
  • 27

1 Answers1

0

Annoyingly, I stumble across the answer as soon as I post this (Despite searching for hours).

You must put @objc(SwiftClassName) above your swift class.
Like:

@objc(SubClass)
class SubClass: SuperClass {...}

Then all of your original Objective-c code will see these classes and not try to do anything funky with them!

Thanks to @klaus

Original post can be found here:

https://stackoverflow.com/a/24448786/846035

Community
  • 1
  • 1
Adrian_H
  • 1,548
  • 1
  • 14
  • 27