Method cannot be marked @objc because its result type cannot be represented in Objective-C
Asked Answered
I

4

12

am exposing swift API's in Objective-C and the Objective-C runtime.

When i add "@objc" before the function throws an error "Method cannot be marked @objc because its result type cannot be represented in Objective-C"

My code is here

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

User is optional struct. how to solve this.

Intimate answered 29/3, 2018 at 9:29 Comment(6)
Show class declaration code for this function (class for this function)Bolten
public final class Manager : NSObject { }Intimate
add your code of UserBend
User is a struct not the class.Intimate
@Intimate target/selector is not possible with swift Struct, if you are using one i suggest you call the selector from any NSObject classPoltergeist
can you post the struct code?Rood
T
11

The key bit of information is this:

User is optional struct

If User is a struct, then it can't be represented in Objective-C, just the same as a Swift class that doesn't inherit from NSObject.

In order for the method logIn(_:) to be able to be marked @objc, then every type referenced in the method declaration has to be representable in Objective-C. You're getting the error message because User isn't.

To fix it, you're either going to have to change the declaration of User from this:

struct User {
    // ...
}

...to this:

class User: NSObject {
    // ...
}

...or redesign logIn(_:) so that it doesn't return a User.


You can find more information about this here. In particular, this answer offers the following potential solution:

The best thing i found was to wrap in a Box class

public class Box<T>: NSObject {
    let unbox: T
    init(_ value: T) {
        self.unbox = value
    }
}
Toluene answered 29/3, 2018 at 10:21 Comment(9)
@Intimate Please look more closely at my code sample. You have to not only add inheritance from NSObject but also change it from a struct to a classMasry
@Intimate You are repeatedly replying to people's answers by saying "User is a struct not the class". You need to change it from a struct to a class; this is the essence of most of the answers.Masry
I want to keep the User struct. I don't want to be make it as class.Intimate
In that case, please note the end of my answer, where I say "or redesign logIn(_:) so that it doesn't return a User." It is not possible to do what you are asking; structs cannot be represented in Objective-C.Masry
@Intimate I've edited my answer to include another potential solution from a different StackOverflow answer.Masry
I don't want to make it as class. As per my requirement it should be struct. Any work around for that.. ?Intimate
@Intimate Yes, that's in the edit I made to my question. You can use the Box class defined at the bottom, and use that to wrap User. I'll warn you, though, your options are quite limited if you want to keep User as a struct, and you might run into additional problems down the line.Masry
Can you help me to explain with an example.. ?Intimate
I don't think the Box solution will work, because generic classes are not visible to Objective-C.Archespore
V
3

Change the definition of your class as below

class User: NSObject {

}

In this way this class will be available in Objective-C

Vendace answered 29/3, 2018 at 9:38 Comment(0)
B
0

Your class or protocol, must be inherited (extended) by NSObject or anyother class in its hierarchy, containing your code (function) with @objc notation.

Example:

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

To use/declare @objc with this function, class must extend NSObject (or any other class in its hierarchy)

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

Update:

struct may not work with optional value in Objective-C, as a work around, you should change User from struct to class

Try following code and see:

public class User : NSObject {
    // Your code
}

public final class Manager : NSObject {

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

Here is snapshot with editor

enter image description here

Bolten answered 29/3, 2018 at 9:37 Comment(2)
User is struct not the class.Intimate
@Intimate struct may not work with optional value in Objective-C, as a work around, you should change User from struct to classBolten
M
-1

Only an NSObject-derived class type can be seen by Objective-C. Use:

class User : NSObject {

}

Meadors answered 29/3, 2018 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.