22

In objective c, I could create a class array and use it in a method

NSArray *classes = @[[NSAttributedString class]];
NSArray *items = [pasteboard readObjectsForClasses:classes
                                           options:nil];

However, swift has no "class" function. Returning a Metatype for a class results in an error

    let aClass : AnyClass!  = NSAttributedString.self
    //or let aClass:AnyClass! = NSAttributedString.classForArchiver()

    let pasteboard = NSPasteboard.generalPasteboard()

    let classes = [aClass]

    let objects = pasteboard.readObjectsForClasses(classes, options: nil)

the final line results in "fatal error: array element cannot be bridged to Objective-C"

How can I create a class array in Swift?

4
  • another way is objc_getClass("NSAttributedString"), but depending on the name of the class as a string is not as good as Sulthan's solution Commented Jun 17, 2014 at 21:40
  • Update: it appears that in Xcode 6 beta 3, this code works with no problem Commented Jul 9, 2014 at 2:32
  • Update: the problem appears again in Xcode 6 beta 6 Commented Aug 30, 2014 at 1:28
  • Update: it seems to be fixed again in Xcode 6.1 beta Commented Sep 22, 2014 at 17:36

3 Answers 3

13

Apparently, this has been fixed in Beta 3 so there is no need for workarounds anymore

I have found a pure Swift hack:

func classFromType<T: NSObject>(type: T.Type) -> AnyObject! {
    return T.valueForKey("self") //ask for class indirectly...
}

var clazz: AnyObject = classFromType(NSAttributedString.self)
var classes = [clazz]

Previous answer

I wasn't able to get the Class instance by any means in Swift. It seems it cannot be converted directly from AnyClass. For example, the following is not possible (will crash at runtime)

var classes: NSMutableArray = []
classes.addObject(NSClassFromString(NSAttributedString.className()))

So I went into Obj-C and defined a simple NSMutableArray category:

@implementation NSMutableArray (ClassArray)

- (void)addClassByName:(NSString *)className {
    [self addObject:NSClassFromString(className)];
}

@end

Then

var classes: NSMutableArray = []
classes.addClassByName(NSAttributedString.className())

works but it is a bit of a hack.

Sign up to request clarification or add additional context in comments.

6 Comments

it appears that in Xcode 6 beta 3, this hack is not necessary anymore
I have this in my code: class MyTeam: NSMutableArray {...} and I want to use swift declaration. How do I do it?
it is not a pure swift cause it requires NSObject as a superclass
@fnc12 Well, this uses to be a hack anyway. You don't need that any more because the bug has been fixed.
@Sulthan it there any way to answer the question without objc?
|
2

in Swift 2.0 you can use

let urls = _pasteboard.readObjectsForClasses([NSURL.self, NSString.self], options: nil)

as <ClassName>.self returns AnyClass value

Comments

1

It rather looks as if the array bridge works on AnyObject, but your aClass is not AnyObject, it's AnyClass! - the stack trace at the error says

Swift._arrayBridgeToObjectiveC <A, B>(Swift.Array<A>) -> Swift.Array<B> ()

and the assembler prior to it stopping looks as if they are deliberately trapping on it not being AnyObject.

Changing your code to

var classAsAnyObject : AnyObject = aClass as AnyObject
//var classes = [aClass]
var classes = [classAsAnyObject]

causes an immediate crash in objc_retain, as does

var array = NSArray(object:aClass)

so I guess it's something to do with ARC and classes.

Handling of Classes as objects themselves in Swift seems obscure, to say the least.

It doesn't answer the real question (how do you create an array of classes), but for the application you used as an example, you could circumvent it by doing something like:

    var pasteboard = NSPasteboard.generalPasteboard()
    let allObjects = pasteboard.pasteboardItems
    println("allObjects=\(allObjects)")
    for item : AnyObject in allObjects {
        println("item=\(object_getClassName(item))")
        if let pbi = item as? NSPasteboardItem {
            println("item.types=\(pbi.types)")
            let str = pbi.stringForType("public.utf8-plain-text")
            println("item as string \(str)")
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.