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

Fix: Easy use of Swift classes in Objective-C projects #4585

Closed
wants to merge 16 commits into from
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixed

- Easy use of Swift classes in Objective-C projects (#4585)

## 8.42.0-beta.1

### Features
Expand All @@ -16,7 +22,6 @@
- Load integration from same binary (#4541)
- Masking for fast animations #4574


### Improvements

- impr: Speed up getBinaryImages V2 (#4539). Follow up on (#4435)
Expand Down
2 changes: 1 addition & 1 deletion Samples/iOS-ObjectiveC/iOS-ObjectiveC/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "AppDelegate.h"
@import CoreData;
@import Sentry;
#import <Sentry/Sentry.h>
Copy link
Contributor Author

@brustolin brustolin Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how we test if the SDK is working with objc.

Before this PR compiling iOS-ObjectiveC with this change would cause an error.

#import <Sentry/SentryOptions+Private.h>

#import "iOS_ObjectiveC-Swift.h"
Expand Down
4 changes: 2 additions & 2 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@
D8B665BC2B95F73200BD0E7B /* SentryPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = D8B665BA2B95F54200BD0E7B /* SentryPrivate.h */; };
D8B76B062808066D000A58C4 /* SentryScreenshotIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8B76B042808060E000A58C4 /* SentryScreenshotIntegrationTests.swift */; };
D8B76B0828081461000A58C4 /* TestSentryScreenShot.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8B76B0728081461000A58C4 /* TestSentryScreenShot.swift */; };
D8BBD32728FD9FC00011F850 /* SentrySwift.h in Headers */ = {isa = PBXBuildFile; fileRef = D8BBD32628FD9FBF0011F850 /* SentrySwift.h */; settings = {ATTRIBUTES = (Private, ); }; };
D8BBD32728FD9FC00011F850 /* SentrySwift.h in Headers */ = {isa = PBXBuildFile; fileRef = D8BBD32628FD9FBF0011F850 /* SentrySwift.h */; settings = {ATTRIBUTES = (Public, ); }; };
D8BC28C82BFF5EBB0054DA4D /* SentryTouchTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8BC28C72BFF5EBB0054DA4D /* SentryTouchTracker.swift */; };
D8BC28CA2BFF68CA0054DA4D /* NumberExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8BC28C92BFF68CA0054DA4D /* NumberExtensions.swift */; };
D8BC28CC2BFF78220054DA4D /* SentryRRWebTouchEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8BC28CB2BFF78220054DA4D /* SentryRRWebTouchEvent.swift */; };
Expand Down Expand Up @@ -1983,7 +1983,7 @@
D8B665BB2B95F5A100BD0E7B /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = module.modulemap; path = Sources/Sentry/include/module.modulemap; sourceTree = SOURCE_ROOT; };
D8B76B042808060E000A58C4 /* SentryScreenshotIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryScreenshotIntegrationTests.swift; sourceTree = "<group>"; };
D8B76B0728081461000A58C4 /* TestSentryScreenShot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentryScreenShot.swift; sourceTree = "<group>"; };
D8BBD32628FD9FBF0011F850 /* SentrySwift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentrySwift.h; path = include/SentrySwift.h; sourceTree = "<group>"; };
D8BBD32628FD9FBF0011F850 /* SentrySwift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentrySwift.h; path = Public/SentrySwift.h; sourceTree = "<group>"; };
D8BC28C72BFF5EBB0054DA4D /* SentryTouchTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTouchTracker.swift; sourceTree = "<group>"; };
D8BC28C92BFF68CA0054DA4D /* NumberExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NumberExtensions.swift; sourceTree = "<group>"; };
D8BC28CB2BFF78220054DA4D /* SentryRRWebTouchEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryRRWebTouchEvent.swift; sourceTree = "<group>"; };
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/Public/Sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ FOUNDATION_EXPORT const unsigned char SentryVersionString[];
# import <Sentry/SentrySpanProtocol.h>
# import <Sentry/SentrySpanStatus.h>
# import <Sentry/SentryStacktrace.h>
# import <Sentry/SentrySwift.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: Doesn't this make all Swift classes public?

I can now access NSArray<NSString *> * features = [SentryEnabledFeaturesBuilder getEnabledFeaturesWithOptions:options]; in the AppDelegate.m from the iOS-Objective-C sample app.

Copy link
Contributor Author

@brustolin brustolin Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does for Objc project.

If you think this is a problem we close the PR, I tried other ways to solve this without success, its this or nothing (until we find something else).
I dont think users will start to use things they dont know how to use. It's already hard to make them use the public APIs that have documentation.

Also, this will be a big problem for some users with special projects, like this one: #4543

Copy link
Member

@philipphofmann philipphofmann Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think users will start to use things they dont know how to use.

Murphy's law: "Anything that can go wrong will go wrong." So someone will use these classes. They always do.

If I'm not mistaken, there is no difference between an actual public class in ObjC and an accidentally public class as SentryEnabledFeaturesBuilder. So, how can I tell the difference between the two in ObjC code? I didn't look closely into this, but maybe a stupid question: Can't we split up the classes into two headers? We can also discuss this on a call, if this turns out the be a longer discussion.

Copy link
Member

@philipphofmann philipphofmann Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, didn't we want to move the SR code into its own module / SPM / Cocoa package? Maybe this would help here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, didn't we want to move the SR code into its own module / SPM / Cocoa package? Maybe this would help here.

Yes, this would be a solution.

So, how can I tell the difference between the two in ObjC code?

You can't, both are public.

Can't we split up the classes into two headers?

No, its an auto generated header by Xcode

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would appreciate it if we didn't make all the Swift classes public to fix this issue, especially if we plan on converting more code to Swift. So even though moving the SR code to its extra package seems like a lot of work to fix this, it's the right thing to do, IMO. It would also reduce the size of the sentry-cocoa lib.

# import <Sentry/SentryThread.h>
# import <Sentry/SentryTraceContext.h>
# import <Sentry/SentryTraceHeader.h>
# import <Sentry/SentryTransactionContext.h>
# import <Sentry/SentryUser.h>
# import <Sentry/SentryUserFeedback.h>
# import <Sentry/SentryWithoutUIKit.h>

#endif // __has_include(<Sentry/Sentry.h>)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#if __has_include(<SentryWithoutUIKit/Sentry.h>)
# if __has_include("SentryWithoutUIKit-Swift.h")
# import "SentryWithoutUIKit-Swift.h"
# else
# elif __has_include(<SentryWithoutUIKit/SentryWithoutUIKit-Swift.h>)
# import <SentryWithoutUIKit/SentryWithoutUIKit-Swift.h>
# endif
#else // !__has_include(<SentryWithoutUIKit/Sentry.h>)
Expand All @@ -31,7 +31,7 @@

# if __has_include("Sentry-Swift.h")
# import "Sentry-Swift.h"
# else
# elif __has_include(<Sentry/Sentry-Swift.h>)
# import <Sentry/Sentry-Swift.h>
# endif
#endif // __has_include(<SentryWithoutUIKit/Sentry.h>)
Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/Public/SentryWithoutUIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ FOUNDATION_EXPORT const unsigned char SentryVersionString[];
# import <SentryWithoutUIKit/SentrySpanProtocol.h>
# import <SentryWithoutUIKit/SentrySpanStatus.h>
# import <SentryWithoutUIKit/SentryStacktrace.h>
# import <SentryWithoutUIKit/SentrySwift.h>
# import <SentryWithoutUIKit/SentryThread.h>
# import <SentryWithoutUIKit/SentryTraceContext.h>
# import <SentryWithoutUIKit/SentryTraceHeader.h>
Expand Down
Loading