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

@objc not working as explained. #82

Closed
aaronrogers opened this issue Mar 13, 2015 · 6 comments
Closed

@objc not working as explained. #82

aaronrogers opened this issue Mar 13, 2015 · 6 comments

Comments

@aaronrogers
Copy link

In the example
@objc (RWTChicken) class Chicken {
The Objective-C header will be created like

SWIFT_CLASS("RWTChicken")
@interface Chicken

So in Objective-C I still have to reference the class as Chicken. Is RWTChicken the intended result?

@marciok
Copy link

marciok commented Mar 19, 2015

When using a Swift class on Objective-C with a custom name you should use the same name as in Swift, on runtime the object will be created with the custom name provided, for example:

Chicken *chicken = [Chicken alloc] init];
NSLog("%@", chicken); // < RWTChicken 0x423432>

I think it was added to the style guide just to make more concise with the Objective-C guide.

More info:
#13
http://petehare.com/adding-obj-c-prefixes-to-swift-class-for-interoperability/
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html

@aaronrogers
Copy link
Author

I guess it makes sense but it just seems odd as using @objc on methods has the opposite effect in that the name provided in @objc for a method is the name used in Objective-C.

How does the above help if there's a Chicken class in two separate modules referenced in the same Objective-C file? How does one specify which Chicken is to be used?

@marciok
Copy link

marciok commented Mar 20, 2015

Sorry I didn't understand your question but as an overall what Apple say about specifying name is:

"... if the name of your Swift class contains a character that isn’t supported by Objective-C, you can provide an alternative name to use in Objective-C..."

So I'm not sure if we should add a custom name, it doesn't seems that was made for that, if you don't add it will generate the object with namespace so you don't risk to have a conflict.

@aaronrogers
Copy link
Author

Ok, my question is:

  • I'm using two third party modules.
  • Each of these modules have a Chicken class.
  • I want to reference both in the same Objective-C file.

How does that work? When I do [Chicken new] which module is used?

@AsceticMonk
Copy link

I agree with @aaronrogers on this, it caused a great deal of confusion when I initially read the section on Class Prefixes.

The way it is written seems to suggest to add the additional name with prefix in the parenthesis, so you could use the class name with prefix in your Objective-C code. However, that's not the case, if one tries to reference the Chicken Swift type with RWTChicken, the code would not compile.

The prefixed class name is only used during runtime. Thus, I think the section should be rewritten to be more clear on this.

@gregheo
Copy link
Contributor

gregheo commented Apr 9, 2015

Our tutorials are all Swift anyway, so you're right to point out the possible confusion here.

@gregheo gregheo closed this as completed in 3862b3b Apr 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants