Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3332] @objc attribute on Linux platform leads to error #45920

Open
sinoru opened this issue Dec 5, 2016 · 10 comments
Open

[SR-3332] @objc attribute on Linux platform leads to error #45920

sinoru opened this issue Dec 5, 2016 · 10 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@sinoru
Copy link

sinoru commented Dec 5, 2016

Previous ID SR-3332
Radar rdar://problem/22974292
Original Reporter @sinoru
Type Bug
Environment

Ubuntu 14.04.5 LTS, Linux Kernel 4.4.0-51-generic, Swift 3.0.1

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 8d8239631adbe9300b1172399e9d57fb

is duplicated by:

  • SR-6399 Compilation failures on linux when adding @objc annotations to types that bridge to ObjC classes
  • SR-7926 Ignore @objc when Objective-C runtime is not available

relates to:

Issue Description:

I know that Objective-C Interoperability not works on linux. But I want to share the only the swift codes on Linux, macOS platforms.
I think it will be great if compiler ignore or just warning on @objc attribute on linux platform than print "only classes that inherit from NSObject can be declared @objc" error.
Thanks.

@belkadan
Copy link
Contributor

belkadan commented Dec 5, 2016

Most of the time you don't need to write @objc explicitly on a class—that's only if you care about its runtime name. If you do care about its runtime name, it seems like the same code wouldn't work on Linux.

@belkadan
Copy link
Contributor

belkadan commented Dec 5, 2016

Can you share the code where this is an issue? Collecting examples might help us decide what to do about it.

@sinoru
Copy link
Author

sinoru commented Dec 6, 2016

Here is the example codes:

@objc(STWUser)
public class User: NSObject {
    
    var jsonObject: [String:Any]
    
    @objc public var id: Int64
    @objc public var screenName: String
    @objc public var name: String? {
        return self.jsonObject["name"] as? String
    }
    @objc public var profileImageURL: URL? {
        return URL(string: self.jsonObject["profile_image_url"] as? String ?? "")
    }
    
    init?(jsonObject: Any) {
        guard let jsonObject = jsonObject as? [String:Any] else {
            return nil
        }
        self.jsonObject = jsonObject
        
        guard let id = jsonObject["id"] as? Int64 else {
            return nil
        }
        self.id = id
        
        guard let screenName = jsonObject["screen_name"] as? String else {
            return nil
        }
        self.screenName = screenName
        
        super.init()
    }
}

And I care the interface of obj-c line for working well with Obj-C. Meantime, without @objc, those codes are works well on Linux platform. I think @objc is only matters when Obj-C runtime is available. Isn't it?

@belkadan
Copy link
Contributor

belkadan commented Dec 6, 2016

Why do you need to rename the class to STWUser in Objective-C? Is this a library that's intended to be used in both Objective-C and Swift?

@sinoru
Copy link
Author

sinoru commented Dec 7, 2016

Yes. I want this library works well on both Obj-C, Swift.

@swift-ci
Copy link
Collaborator

Comment by Joe Charlier (JIRA)

I have a library that uses KVC and KVO that I'm trying to get working on Linux. This error is of course coming up on all String and Date properties. Is there anyway around this? Is KVC and KVO unavailable on linux swift?

@sinoru
Copy link
Author

sinoru commented Jan 15, 2018

aepryus (JIRA User): KVO, KVC is the features of Obj-C's Foundation which is not implemented in Linux. I suggest these for who build the framework targeting Linux, macOS both. (Which I intend to make swift ignore `@objc` annotation on Linux. Not makes the error.)

@swift-ci
Copy link
Collaborator

Comment by Joe Charlier (JIRA)

@sinoru: I actually need the KVC functionality in this case, so simply ignoring the warning without getting String and Date working with KVC won't be very helpful in my case.

@ffried
Copy link
Contributor

ffried commented Sep 19, 2018

I also came across this issue today. First of all, the error message is misleading. Given @objc class ABC: NSObject {{{}}} it's clearly visible that it does inherit from NSObject. No matter how @objc attributes are handled, the error message should currently state, that the ObjC runtime is not available on Linux.

Apart from that, I need the attribute in a test where we also care about the runtime name of the class. The tested logic involves NSStringFromClass which uses the @objc name on Darwin platforms. Currently, in our case it seems like the @objc attribute could just be ignored on Linux, since NSStringFromClass returns String(describing: cls) there.

@kam800
Copy link

kam800 commented May 3, 2019

Hi. I came across the same case as described in this issue – sharing code for iOS app and Swift backend. I managed to clean @objc annotations for backend-build using following script:

#!/bin/bash

DIR=$(dirname "${BASH_SOURCE[0]}")

find "${DIR}/../Sources" -name '*.swift' | while read SWIFT_FILE
do
  sed -i'' 's/@objc([^)]*)//g' "${SWIFT_FILE}"
  sed -i'' 's/@objc//g' "${SWIFT_FILE}"
done

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

5 participants