Advertisement
beng

Compiling Swift and Objective-C from the Command-Line

Sep 26th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.95 KB | None | 0 0
  1. % ls -l
  2. total 40
  3. -rw-r--r--@ 1 bhamiltoncx  staff  111 Sep 26 14:42 ObjcGreeter.h
  4. -rw-r--r--@ 1 bhamiltoncx  staff  155 Sep 26 14:50 ObjcGreeter.m
  5. -rw-r--r--  1 bhamiltoncx  staff  300 Sep 26 15:06 greetings.swift
  6. -rw-r--r--  1 bhamiltoncx  staff  304 Sep 26 15:06 input.swift
  7. -rw-r--r--  1 bhamiltoncx  staff  165 Sep 26 15:06 main.swift
  8.  
  9. % cat greetings.swift
  10. import Foundation
  11.  
  12. public func sayHello(name: String) {
  13.      print("Hello " + name)
  14. }
  15.  
  16. @objc public class Greeter : NSObject {
  17.     public class func sayHello(name: String) {
  18.         // To access the global function, you have to prefix it with the module name
  19.         greetings.sayHello(name)
  20.     }
  21. }
  22.  
  23. % cat input.swift
  24. import Foundation
  25.  
  26. public func getInput() -> String? {
  27.   let stdin = NSFileHandle.fileHandleWithStandardInput()
  28.   let data = stdin.availableData
  29.   let str = NSString(data: data, encoding: NSUTF8StringEncoding)
  30.   let trimmedStr = str?.stringByTrimmingCharactersInSet(
  31.       NSCharacterSet.whitespaceAndNewlineCharacterSet())
  32.   return trimmedStr as String?
  33. }
  34.  
  35. % cat ObjcGreeter.h
  36. #import <Foundation/Foundation.h>
  37.  
  38. @interface ObjcGreeter : NSObject
  39.  
  40. + (void)sayHello:(NSString *)name;
  41.  
  42. @end
  43.  
  44. % cat ObjcGreeter.m
  45. #import "ObjcGreeter.h"
  46. // Generated by the Swift compiler
  47. #import "greetings-Swift.h"
  48.  
  49. @implementation ObjcGreeter
  50.  
  51. + (void)sayHello:(NSString *)name {
  52.     [Greeter sayHello:name];
  53. }
  54.  
  55. @end
  56.  
  57. % cat main.swift
  58. import greetings
  59.  
  60. print("What is your name?")
  61. let name = getInput() ?? "(unable to decode input)"
  62. sayHello(name)
  63.  
  64. ObjcGreeter.sayHello("to Swift from Objective-C")
  65.  
  66.  
  67. % swift -frontend -c -module-name greetings -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -emit-module -emit-module-path greetings.swiftmodule -enable-objc-interop -emit-objc-header -emit-objc-header-path greetings-Swift.h -c -o libgreetings.o greetings.swift input.swift
  68.  
  69. % clang -isysroot `xcrun --sdk macosx --show-sdk-path` -mmacosx-version-min=10.11 -c -I . ObjcGreeter.m
  70.  
  71. % swift -frontend -c -I . -c -primary-file main.swift -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -enable-objc-interop -import-objc-header ObjcGreeter.h main.swift    
  72.                                                                                                      
  73. % clang -isysroot `xcrun --sdk macosx --show-sdk-path` -mmacosx-version-min=10.11 -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx -Xlinker -force_load_swift_libs -lswiftRuntime -lc++ -framework Foundation -Xlinker -add_ast_path -Xlinker greetings.swiftmodule -o hello main.o libgreetings.o ObjcGreeter.o
  74.  
  75. % ./hello
  76. What is your name?
  77. Ben
  78. Hello Ben
  79. Hello to Swift from Objective-C
  80.  
  81. % printf "\xAB\xCD\xEF" | ./hello
  82. What is your name?
  83. Hello (unable to decode input)
  84. Hello to Swift from Objective-C
  85.  
  86. % echo 💩 | ./hello              
  87. What is your name?
  88. Hello 💩
  89. Hello to Swift from Objective-C
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement