3

I would like to check in swift whether an object is a member of a specific class. However the class is stored as property of an another object, in this case RKRequestDescriptor, a class of the RESTKit framework. RKRequestDescriptor has a property defined, which stores a class object in its objectClass property.

So in objective-c I have something like:

  for (RKRequestDescriptor *thisDescr in self.requestDescriptors) {
    if ([thisDescr objectClass] == [obj class]) {
        ...

where "obj" is some arbitrary object to test against and since this is a subclass of RESTKits RKObjectManager, there is an array with requestDescriptors.

Now I am having a hard time in swift to "translate" the above objective-c code. I tried:

     for var thisDescr:RKRequestDescriptor in self.requestDescriptors {
        if let newObj = obj as thisDescr.objectClass {

That doesn't work. Xcode throws errors. To begin with it doesn't "like" in-the loop ("expected condition in for statement). So it to:

    var thisDescr:RKRequestDescriptor?
    for index in 0..<countElements(self.requestDescriptors) {
        thisDescr = self.requestDescriptors[index] as? RKRequestDescriptor
        if let newObj:AnyObject = obj as? thisDescr.objectClass {

but still no luck. It is really harder than I thought to "translate" objective c to swift :-(

alex da franca
  • 1,450
  • 2
  • 15
  • 27
  • http://stackoverflow.com/questions/24101450/how-do-you-find-out-the-type-of-an-object-in-swift – 67cherries Dec 16 '14 at 14:20
  • thank you, but I had seen that SO question, but it didn't help me, as I want to compare to an arbitrary class, which is stored in a variable. Or am overlooking something? – alex da franca Dec 16 '14 at 14:23
  • It is not "hard" to translate Objective-C to Swift at all. In fact, a machine could do it. It is pure application of simple rules. But you do have to speak in Swift syntax; you can't just say any old nonsense you like. – matt Dec 16 '14 at 14:29
  • Oh, that's good news. So such a machine will be 'round the corner soon, if that's feasible. You should think about coming up with one yourself then. Might even be a good business idea. – alex da franca Dec 16 '14 at 14:45
  • I have in fact suggested this to Apple. In a sense Apple is doing it in part (that is, after all, what the Swift headers are). They are much of the way there already! There's no money to be made with it, but it would make a useful utility, similar to the other Edit > Refactor functionality that Xcode already has. – matt Dec 16 '14 at 14:55
  • It might help to get used to the syntax. But yes, probably no money to make with it, as it isn't really necessary technically, only helps for learning purposes. – alex da franca Dec 16 '14 at 15:12
  • I've translated thousands of lines of my own code from Objective-C to Swift, and believe me, I could tell that a machine could have done it! I was just a coding monkey. Worse than a monkey. A coding amoeba. – matt Dec 16 '14 at 15:14
  • That gives me hope :-) I have just started the job of translating parts of my code, as I want to move forward and not stick to an "old" language and the clients also are starting to request swift code. ...but then again, I learned Latin as well, which qualifies as old language... ;-) – alex da franca Dec 16 '14 at 15:24
  • I know Latin and Ancient Greek, and believe me, translating Objective-C is easier! :) – matt Dec 20 '14 at 17:59

1 Answers1

3

Swift equivalent of [thisDescr objectClass] == [obj class] is:

thisDescr.objectClass === obj.dynamicType
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • thanks, that works like a champ. As for the other problem with the loop, I figured out, that it works, if I leave out the "var" keyword. It makes sense, as swift will implicitly treat it as a variable in a for..in loop. – alex da franca Dec 16 '14 at 14:40
  • @alexdafranca Yup, that's what I was referring to when I said "speak in Swift syntax". – matt Dec 16 '14 at 14:57
  • @matt Yes, of course I need to get used to the syntax. Unfortunately at the moment that is much of time consuming "try and error", as things like these are not immediately "logical" to me. Until now all other places, where swift is implicit and tries to be smart in order to save us keystrokes (and typos or especially: errors!) – alex da franca Dec 16 '14 at 15:04
  • @alexdafranca Apple's documentation is _excellent_ and very complete, and states clearly and simply what the syntax for `for...in` is. – matt Dec 16 '14 at 15:07
  • (...ups, that return key again, sorry) ...in those areas it usually is allowed to be explicit. So if the thing is meant to be a "var" I can omit it, but I am used to be able to write it nonetheless, for what reason ever (readability etc.). But in this case the compiler complains with an unhelpful error message ("Expected '{' in for statement"). – alex da franca Dec 16 '14 at 15:08
  • File a bug with Apple about the unhelpful error message! (If you have time.) This is a major problem with Swift; the error messages are very opaque, and often cause you to look in the wrong place for the source of the issue. – matt Dec 16 '14 at 15:15