A method cannot be marked as @objc because its result type cannot be represented in Objective-C

am exposes the fast API in Objective-C and Objective-C runtime.

When I add "@objc" before the function throws an error, "The method cannot be marked as @objc because its result type cannot be represented in Objective-C"

My code is here

@objc public static func logIn(_ userId: String) -> User? { }

The user is an optional struct. how to solve it.

+4
source share
4 answers

Your class or protocol must be inherited (extended) using NSObjector any other class in its hierarchy containing your code (function) with a note @objc.

Example:

class TestClass {
    public static func logIn(_ userId: String) -> User? { }
}

/declare @objc , NSObject ( )

class TestClass {
    @objc public static func logIn(_ userId: String) -> User? { }
}

:

struct Objective-C, , User struct class

:

public class User : NSObject {
    // Your code
}

public final class Manager : NSObject {

    @objc public static func logIn(_ userId: String) -> User? {
        return nil
    }
}

enter image description here

0

class User: NSObject {

}

, Objective-C

0

:

struct

User - , ​​ Objective-C, , Swift, NSObject.

logIn(_:) @objc, , , Objective-C. , User .

, User :

struct User {
    // ...
}

... :

class User: NSObject {
    // ...
}

... logIn(_:), User.


. , :

, , - Box

public class Box<T>: NSObject {
    let unbox: T
    init(_ value: T) {
        self.unbox = value
    }
}
0

Only the type of class based on NSObject can be seen with Objective-C. Using:

class User : NSObject {

}

-2
source

Source: https://habr.com/ru/post/1695516/


All Articles