Questions tagged [objective-c-swift-bridge]

Swift’s compatibility with C and Objective-C lets you create a project that contains files written in either language.

Official documentation.

117 questions
5
votes
2 answers

React Native Bridging: Passing JSON to Swift function

Im a rookie in Swift and Objective-C but Im trying to make a bridge from React Native to Swift, and send a JSON and a JSON Array as params to Swift. Inside React I would like to call this function: startTrack('some url string', { artist: 'Bruce…
5
votes
1 answer

NS_SWIFT_NAME for Importing C function as a class extension

I have two C functions: extern UIColor* LNRandomDarkColor(); extern UIColor* LNRandomLightColor(); As an exercise, I am trying to import them into Swift as extension methods to UIColor. Following Apple's "What's New in Swift" presentation from WWDC…
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
5
votes
2 answers

Is it possible to cast "AnyObject" to a swift struct

I have a swift method which receives a struct as a parameter. Since the structs aren't bridged to objective-c, this method is invisible in the bridging header. I was forced to created a new "identical" method that receives "AnyObject" instead of the…
Avba
  • 14,822
  • 20
  • 92
  • 192
5
votes
2 answers

NSClassFromString using a Swift File

I have written a class in Swift in my existing Objective-C project. So far, the bridging has worked very well. I do have a method however, where I generate a class at runtime using NSClassFromString(). When the string is my Swift class name, it…
ahoang
  • 139
  • 1
  • 9
5
votes
1 answer

'LOG_INFO' macro redefined warning after moved project to swift bridging project

I got several warnings like these: Swift compiler warning: CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler /Myfolder/Pods/Headers/CocoaLumberjack/DDLog.h:176:9: 'LOG_INFO' macro…
angelokh
  • 9,426
  • 9
  • 69
  • 139
4
votes
1 answer

How do I create an Objective-C bridging header?

I'm following this guide on adding OneSignal to React Native, and there is one section where is says: Open NotificationService.m or NotificationService.swift and replace the whole file contents with the code below: If you are…
gkeenley
  • 6,088
  • 8
  • 54
  • 129
4
votes
1 answer

Init a `sockaddr` struct with a given IP address in Swift

I playing around with Apple's Reachabilitty class in Swift. The web server in my private Wi-Fi runs on http://10.0.0.1. To check its reachability I want to use the following function from Reachability.h (not the one with hostname): /*! * Use to…
winklerrr
  • 13,026
  • 8
  • 71
  • 88
4
votes
1 answer

Objective-C call Swift function

Swift function defined in MySwift.swift File: func SomeSwift() { } SomeSwift() is not defined in any Swift class, it is just a pure function. After CMD + B to build the project, open Project-Swift.h, the SomeSwift() isn't show in there. Does the…
al03
  • 144
  • 1
  • 9
4
votes
1 answer

Import Swift code to Objective-C file

I have created a Swift project and imported Objective-C files. During import Xcode asked me to create a bridging header and created a header. Now in the Objective-C files of the same project I want to use Swift class methods. I googled about…
Rakesh
  • 1,177
  • 1
  • 15
  • 31
3
votes
2 answers

Why do properties modified by @optional become immutable?

I have an Objective-C protocol that contains a property as follows: #import @protocol Playback @optional @property (nonatomic, nonnull) NSURL *assetURL; @end PlayerController has a property of type…
3
votes
1 answer

Using Swift Project (Siren) in an Objective-C iOS Application

I am trying to use a popular swift library, Siren, in an iOS Objective-C application. I have included the library as a framework using cocoapods, I ran pod init and then pod install with a podfile like this: # Uncomment the next line to define a…
mogelbuster
  • 1,066
  • 9
  • 19
3
votes
0 answers

"Failed to import bridging header" iOS Unit tests

I have an iOS project with 2 development targets and 1 unit test target. The development targets are hybrid (using Objective-c and Swift at the same time), thus I have to use Bridging-Header.h within the project. The problem is when I want to import…
3
votes
0 answers

Guidelines to nullability when bridging Objective-C code

I'm currently a novice in Swift, but I'm a maintainer of an Objective-C framework which is a wrapper around a C library, for which I'd like to have the nicest API possible. I'm looking at this kind of code /// The index checksum @property…
tiennou
  • 487
  • 3
  • 13
3
votes
2 answers

Bridge Class Functions in React Native

Say I have a class called ExampleClass. Say I then write code like so: @objc(ExampleClass) class ExampleClass: NSObject { @objc class func exampleFunc() -> Void { } } With an Objective-C file header like so: #import…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
3
votes
1 answer

KVO for dependent key paths does not work properly for Swift class

I'm trying to write a wrapper around URLSessionTask in Swift. According to the documentation All task properties support key-value observing. So I want to keep this behavior and make all the properties on my wrapper also KVO-compliant (usually…