Getting Started
- Inside a Swift project select File -> New -> File .. [or cmd + N]
- Select iOS -> Cocoa Touch Class
- Provide class name (e.g. HelloSwift), set to Subclass of NSObject and set language to Objective-C
- Click Next
- Agree to Bridging Header
- Navigate to Bridging Header: YourProjectName-Bridging-Header.h
- Insert the following text into Bridging Header: #import "HelloSwift.h"
Adding the code
Place the following text in your HelloSwift.h file:#import <Foundation/Foundation.h> @interface HelloSwift : NSObject -(NSString *)helloObjectiveC:(NSString *)name; @end
Place this code in your HelloSwift.m file:
#import "HelloSwift.h" @implementation HelloSwift -(NSString *)helloObjectiveC:(NSString *)name { NSString *myString = [NSString stringWithFormat:@"Hello %@, my name is Objective-C", name]; return myString; } @end
Inside the viewDidLoad of your initial view controller class file add the following lines of Swift:
var myHello = HelloSwift() println(myHello.helloObjectiveC("Swift"))
Build and run.
Comments
Post a Comment