swift和oc混编_Nullable和_Nonnull

在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一样
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值