在swift和oc混编的时候,在swift中调用oc代码的时候总是会有optional,必须要解包,很麻烦。
在xcode6.3中添加新特性_Nullable(可空)和_Nonnull(非空),只要类型是对象和指针就可以用使用。
oc:
#import <Foundation/Foundation.h>
typedef void (^StrBlock)(NSString* );
@interface NonClass : NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)void(^myBlock)(NSInteger);
-(NSInteger)funcInteger:(NSInteger)a;
-( NSString *)changeInteger:(NSInteger)k;
-(void)blockFunc:(StrBlock )b;//在调用时直接b(@"aaaaa")
@end
在swift调用
let n = NonClass()
n.blockFunc { (a) in
print(a)
}
会打印出
Optional("aaaaa")
当block中传NULL时候,在swift中就会变成nil
当在block定义中加_Nonnull后oc代码变成
typedef void (^StrBlock)(NSString* _Nonnull);
swift调用就会直接打印aaaaa
当在oc中传入NULL时候,在swift中会打印“”空字符串。
当想要添加一个区域内都添加非空属性可以用宏
NS_ASSUME_NONNULL_BEGIN
NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_BEGIN
typedef void (^StrBlock)(NSString* );
@interface NonClass : NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)void(^myBlock)(NSInteger);
-(NSInteger)funcInteger:(NSInteger)a;
-( NSString *)changeInteger:(NSInteger)k;
-(void)blockFunc:(StrBlock )b;
@end
NS_ASSUME_NONNULL_END
使用效果和_Nonnull一样