All classes subclassed by NSObject, the methods will be exposed to objective-c
All methods are not private
All methods are not using swift feature
Protocol must be in @objc
Return type must be objective-c understand not (Int, String)? in Swift
anything marked as @IBOutlet, @IBAction and @NSManaged will be available in Objective-C
dynamic in swift for any property will be obversed by KVO also transfer to Obj-c
@objc for anything expose to objective-c
class CalculatorController : UIViewController {
func performOperation(op: (Double) -> Double) {
// ...
}
//swift knows diffenert function as agruments have different types but not for Obj-c. ERROR in this function
func performOperation(op: (Double, Double) -> Double) {
// ...
}
//this is okay
func performBinaryOperation(op: (Double, Double) -> Double) {
// ...
}
//not expose to swift
@nonObjc
func performOperation(op: (Double, Double) -> Double) {
// ...
}
}
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;
is same as
func contentsForType(typeName: String) throws -> AnyObject
which both languages can call each other
@objc enum RequestError : Int, ErrorType {
case Incomplete = 9001
}
// the enum error from Swift will give the same error in objc
NSError *error;
id result = [controller sendRequest:request error:&error];
if (!result) {
//failure MyApp.RequestError: 9001
NSLog(@"failure %@: %ld", error.domain, error.code);
return nil;
}
// Generated by Swift 2.0.
typedef NS_ENUM(NSInteger, RequestError) {
RequestErrorIncomplete = 9001
};
static NSString * const RequestErrorDomain = @"...";
Compiler gives warning if nil is not expected
Use audited Region for wrap all property are not nil
evaluate values is nullable or not first. and then all the elements in the values array must not nil
@property (nonatomic, strong) NSArray<NSString *> stringArray;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> stringDict;
NSArray<NSString *> *strings = …;
NSArray *array = …;
array = strings; // okay, drops type arguments
strings = array; // okay, adds type arguments
__kindof
to tell the instance is kind of a class or a subclass__kindof
id
in an API?id
has been replaced with more precise type
instancetype
for returning self__kindof x *
for "some subclass of xid<SomeProtocol>
for any type conform to SomeProtocol
Use id
when there is needed for representing any objects
@property (nullable, copy) NSDictionary<NSString *, id> *userInfo;