Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a class array in Swift

Tags:

class

swift

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?

like image 915
Zag Avatar asked Jun 11 '14 23:06

Zag


3 Answers

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.

like image 85
Sulthan Avatar answered Oct 28 '22 10:10

Sulthan


in Swift 2.0 you can use

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

as <ClassName>.self returns AnyClass value

like image 35
medvedNick Avatar answered Oct 28 '22 10:10

medvedNick


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)")
        }
    }
like image 1
Grimxn Avatar answered Oct 28 '22 11:10

Grimxn