Objective-C calling parameterized Swift method crashes Swift compiler

Kendall Helmstetter Gelner

I have a simple Swift extension on NSManagedObject, in which I have a parametrized method for finding a single object - the signature looks like:

public class func findFirst<T:NSManagedObject>(inContext context : NSManagedObjectContext? = .None) -> T?

I'm trying to call this from Objective-C, but it seems like it cannot be seen. If I create a non-parameterized version I can see and call it just fine from Objective-C:

public class func findFirstUntypedWithPredicate(predicate:NSPredicate?, inContext context : NSManagedObjectContext? = .None) -> NSManagedObject?

Is there any way for ObjectiveC to be able to reach the parameterized version of the call?

I would use Self like so:

public class func findFirst(inContext context : NSManagedObjectContext? = .None) -> Self?

using the technique found here:

How can I create instances of managed object subclasses in a NSManagedObject Swift extension?

However, that causes the Swift compiler to segfault when compiling the code (Xcode 6.3.1, or Xcode 6.4 beta 2).

Edit: Here's a link with the full source of the framework I'm trying to build, including bonus Swift compiler crashes caused by templated methods:

https://www.dropbox.com/s/fixaj9ygdoi4arp/KiGiCoreData.zip?dl=0

Martin R

Generic methods are not visible from Objective-C. However you can use the ideas from How to use generic types to get object with same type to define a findFirst() class method which returns Self? (the Swift equivalent of instancetype) without being generic:

// Used to cast `AnyObject?` to `Self?`, `T` is inferred from the context.
func objcast<T>(obj: AnyObject?) -> T? {
    return obj as! T?
}

extension NSManagedObject
{
    class func entityName() -> String {
        let classString = NSStringFromClass(self)
        // The entity is the last component of dot-separated class name:
        let components = split(classString) { $0 == "." }
        return components.last ?? classString
    }

    // Return any matching object, or `nil` if none exists or an error occurred
    class func findFirst(context : NSManagedObjectContext, withPredicate pred : NSPredicate?) -> Self? {
        let name = entityName()
        let request = NSFetchRequest(entityName: name)
        request.predicate = pred
        var error : NSError?
        let result = context.executeFetchRequest(request, error: &error)
        if let objects = result  {
            return objcast(objects.first)
        } else {
            println("Fetch failed: \(error?.localizedDescription)")
            return nil
        }
    }
}

This can be used from Swift

if let obj = YourEntity.findFirst(context, withPredicate: nil) {
    // found
} else {
    // not found
}

and from Objective-C:

YourEntity *obj = [YourEntity findFirst:context withPredicate:nil];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Objective - C delegate method in swift

From Dev

Calling Objective c method in swift app

From Dev

Swift compiler vs Objective-C compiler

From Dev

Calling a Method on an Objective-C Delegate from Swift

From Dev

Calling a Objective-C method using Swift 3

From Dev

Swift: Converting this Objective C Method

From Dev

Calling Swift from Objective C, closure to block?

From Dev

Calling Objective-C function in swift with a closure

From Dev

Creating an object in Swift using the Objective-C factory method gives a compiler error

From Dev

'does not have member' when calling objective C setter method from Swift

From Dev

Calling Objective C method with completion handler from Swift3 (completion is IUO?)

From Dev

Swift: Method overriding in parameterized class

From Dev

@objc protocol crashes the swift compiler

From Dev

@objc protocol crashes the swift compiler

From Dev

Swift - Objective-C load class method?

From Dev

Swift #selector referencing to an Objective-C method

From Dev

Swift method to return a tuple in objective c class

From Dev

How to call an Objective C class method in Swift

From Dev

Override Objective C class method in Swift

From Dev

how to override objective-c method in swift

From Dev

Swift 1.2 redeclares Objective-C method

From Dev

Cannot invoke an Objective-C method in Swift

From Dev

Objective-C protocol method invisible in Swift

From Dev

Converting objective-c method to swift [updateSearchResultsForSearchController]

From Dev

Overloading Method in Swift Gives Objective C error

From Dev

How to call swift method from objective c

From Dev

Objective C class method call in Swift

From Dev

Swift closure crashes when called as Objective-C block

From Dev

While calling a no parameterized function wants a parameter swift

Related Related

  1. 1

    Calling Objective - C delegate method in swift

  2. 2

    Calling Objective c method in swift app

  3. 3

    Swift compiler vs Objective-C compiler

  4. 4

    Calling a Method on an Objective-C Delegate from Swift

  5. 5

    Calling a Objective-C method using Swift 3

  6. 6

    Swift: Converting this Objective C Method

  7. 7

    Calling Swift from Objective C, closure to block?

  8. 8

    Calling Objective-C function in swift with a closure

  9. 9

    Creating an object in Swift using the Objective-C factory method gives a compiler error

  10. 10

    'does not have member' when calling objective C setter method from Swift

  11. 11

    Calling Objective C method with completion handler from Swift3 (completion is IUO?)

  12. 12

    Swift: Method overriding in parameterized class

  13. 13

    @objc protocol crashes the swift compiler

  14. 14

    @objc protocol crashes the swift compiler

  15. 15

    Swift - Objective-C load class method?

  16. 16

    Swift #selector referencing to an Objective-C method

  17. 17

    Swift method to return a tuple in objective c class

  18. 18

    How to call an Objective C class method in Swift

  19. 19

    Override Objective C class method in Swift

  20. 20

    how to override objective-c method in swift

  21. 21

    Swift 1.2 redeclares Objective-C method

  22. 22

    Cannot invoke an Objective-C method in Swift

  23. 23

    Objective-C protocol method invisible in Swift

  24. 24

    Converting objective-c method to swift [updateSearchResultsForSearchController]

  25. 25

    Overloading Method in Swift Gives Objective C error

  26. 26

    How to call swift method from objective c

  27. 27

    Objective C class method call in Swift

  28. 28

    Swift closure crashes when called as Objective-C block

  29. 29

    While calling a no parameterized function wants a parameter swift

HotTag

Archive