idea
- uses modules and headers.
- Fields and headers for methods are defined in an interface in headers (~ like C++):
<a href="/tags/ifndef.html">#ifndef</a> Torture_h
<a href="/tags/define.html">#define</a> Torture_h
@interface Torture: NSObject
@property NSString *instrument;
- (id) initWithInstrument:(NSString*) instrument;
- (NSString*)applyToVictim:(NSString*)name;
@end
<a href="/tags/endif.html">#endif</a> /* Torture_h */
- Implementation goes in a
.m:- Constructor is something returning type
id - Calls between methods are with that weird messaging syntax using
[...]<a href="/tags/import.html">#import</a> <Foundation/Foundation.h> <a href="/tags/import.html">#import</a> "Torture.h"
- Constructor is something returning type
@implementation Torture
- (id) initWithInstrument:(NSString*) instrument { self.instrument = instrument; return self; }
- (NSString)applyToVictim:(NSString)name {
return [NSString stringWithFormat: @"%@ took %@", name, self.instrument];
}
@end
Interop
Use objc in swift
Interop with Swift is done by adding #imports to the bridging header file:
<a href="/tags/import.html">#import</a> "Torture.h"
Then the class can simply be instantiated and called.
Use swift in objc
Use modifier @objc on the swift class.
In the objc .m file, add #import "(projectname)-Swift.h (Project name, not class name). In the objc .h file add @class (classname);