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


(Iliya Trub) #1

Everybody knows, that is very simple in XCode, there are many tutorials, for example,
http://stackoverflow.com/questions/24002369/how-to-call-objective-c-from-swift
or
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

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

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

    @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?


(Joe Groff) #2

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,
http://stackoverflow.com/questions/24002369/how-to-call-objective-c-from-swift
or
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

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

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

   @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?


(Iliya Trub) #3

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?


(Wallacy) #4

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