# Using Objective C class in Swift (open source, Linux)

**URL:** <https://forums.swift.org/t/using-objective-c-class-in-swift-open-source-linux/1275>\
**Category:** Compiler\
**Created:** [February 1, 2016, 12:46pm UTC](https://forums.swift.org/t/using-objective-c-class-in-swift-open-source-linux/1275 "2016-02-01T12:46:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iliya\_Trub](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Iliya\_Trub](https://forums.swift.org/u/Iliya_Trub)\
**Post date:** [February 1, 2016, 12:46pm UTC](https://forums.swift.org/t/using-objective-c-class-in-swift-open-source-linux/1275/1 "2016-02-01T12:46:26Z")

</div>

Everybody knows, that is very simple in XCode, there are many tutorials, for example,

> <https://stackoverflow.com/questions/24002369/how-do-i-call-objective-c-code-from-swift>

or

> **[GitHub - gergelyorosz/swift-objective-c-mixed-project: Creating a mixed Swift...](https://github.com/gergelyorosz/swift-objective-c-mixed-project)**
>
> Creating a mixed Swift 2.0 & Objective C project. Contribute to gergelyorosz/swift-objective-c-mixed-project development by creating an account on GitHub.

But I read in some sources, that that is impossible in Linux (I have Ubuntu 15) for open source swift compiler. Is it true?  
I mean, for example, such simple code:  
file C.h  
#import \<Foundation/NSObject.h\>  
@interface C : NSObject  
@property (copy) NSString \*cu;  
@end

file C.m  
#import "C.h"  
@implementation C

&nbsp;&nbsp;&nbsp;- (id)init { self = [super init]; self.cu = @"Hello world!"; return self; }

&nbsp;&nbsp;&nbsp;- (void)setCu:(NSString \*)str { self.cu = str; }

&nbsp;&nbsp;&nbsp;&nbsp;@end

file s.swift  
let c = C()  
print(c.cu)

I can easily compile C.m in such way:  
clang++ C.m -c -I\<paths to headers on my host\> -fblocks -o C.o

But how to import C.h to s.swift in order to avoid:

s.swift:2:9: error: use of unresolved identifier 'C'  
let c = C()  
We have not bridge-technology as in XCode.

In what way open source swift compiler could be developed to implement such feature?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [February 1, 2016, 4:44pm UTC](https://forums.swift.org/t/using-objective-c-class-in-swift-open-source-linux/1275/2 "2016-02-01T16:44:00Z")

</div>

Swift only implements ObjC interop with Apple's ObjC runtime on iOS/tvOS/watchOS and 64-bit Mac. It could be ported to other runtimes in theory, but someone would have to do that work.

-Joe

> **···**
>
> > On Feb 1, 2016, at 4:46 AM, Труб Илья via swift-dev \<swift-dev@swift.org\> wrote:
> > 
> > Everybody knows, that is very simple in XCode, there are many tutorials, for example,  
> > [How do I call Objective-C code from Swift? - Stack Overflow](http://stackoverflow.com/questions/24002369/how-to-call-objective-c-from-swift)  
> > or  
> > [GitHub - gergelyorosz/swift-objective-c-mixed-project: Creating a mixed Swift 2.0 & Objective C project](https://github.com/gergelyorosz/swift-objective-c-mixed-project)
> > 
> > But I read in some sources, that that is impossible in Linux (I have Ubuntu 15) for open source swift compiler. Is it true?  
> > I mean, for example, such simple code:  
> > file C.h  
> > #import \<Foundation/NSObject.h\>  
> > @interface C : NSObject  
> > @property (copy) NSString \*cu;  
> > @end
> > 
> > file C.m  
> > #import "C.h"  
> > @implementation C
> > 
> > &nbsp;&nbsp;- (id)init { self = [super init]; self.cu = @"Hello world!"; return self; }
> > 
> > &nbsp;&nbsp;- (void)setCu:(NSString \*)str { self.cu = str; }
> > 
> > &nbsp;&nbsp;&nbsp;@end
> > 
> > file s.swift  
> > let c = C()  
> > print(c.cu)
> > 
> > I can easily compile C.m in such way:  
> > clang++ C.m -c -I\<paths to headers on my host\> -fblocks -o C.o
> > 
> > But how to import C.h to s.swift in order to avoid:
> > 
> > s.swift:2:9: error: use of unresolved identifier 'C'  
> > let c = C()  
> > We have not bridge-technology as in XCode.
> > 
> > In what way open source swift compiler could be developed to implement such feature?

---

<div class="post-metadata">

**Author:** ![Iliya\_Trub](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Iliya\_Trub](https://forums.swift.org/u/Iliya_Trub)\
**Post date:** [February 1, 2016, 3:09pm UTC](https://forums.swift.org/t/using-objective-c-class-in-swift-open-source-linux/1275/3 "2016-02-01T15:09:42Z")

</div>

I'd like to note some interesting issue. If I try to input

swiftc -c s.swift -Xcc -I\<paths to my headers, including path to Foundation/NSObject.h\> -import-objc-header C.h

then I get a lot of compile errors on ALL ObjectiveC code lines in NSObject.h, so, it seems that compiler does not recognize Objective C syntax at all.  
But, if I (in another example) use -import-objc-header \<pure C header file\>  
that all is OK: .swift source file with function invocations from pure C .h-file compiled successfully, .o-file is created. What is explanation of such phenomena and how it could be overcomed?

---

<div class="post-metadata">

**Author:** ![Wallacy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wallacy/32/1097_2.png) [@Wallacy](https://forums.swift.org/u/Wallacy)\
**Post date:** [February 1, 2016, 3:29pm UTC](https://forums.swift.org/t/using-objective-c-class-in-swift-open-source-linux/1275/4 "2016-02-01T15:29:57Z")

</div>

Objective-C is not avaliable on Linux.

> **···**
>
> Em seg, 1 de fev de 2016 às 13:09, Труб Илья \<swift-dev@swift.org\> escreveu:
> 
> > I'd like to note some interesting issue. If I try to input
> > 
> > swiftc -c s.swift -Xcc -I\<paths to my headers, including path to  
> > Foundation/NSObject.h\> -import-objc-header C.h
> > 
> > then I get a lot of compile errors on ALL ObjectiveC code lines in  
> > NSObject.h, so, it seems that compiler does not recognize Objective C  
> > syntax at all.  
> > But, if I (in another example) use -import-objc-header \<pure C header file\>  
> > that all is OK: .swift source file with function invocations from pure C  
> > .h-file compiled successfully, .o-file is created. What is explanation of  
> > such phenomena and how it could be overcomed?
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-dev mailing list  
> > swift-dev@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
