1.Block
OC
A 类
1.声明typedef void(^JDKitOpenChatCallback)(NSDictionary *chatInfo);
@property (nonatomic, copy) JDKitOpenChatCallback openChatCallback;
2.实现
if (self.openChatCallback) {
self.openChatCallback(xxx);
}
B 类
3.回调
[JDKitManager shareInstance].openChatCallback = ^(NSDictionary *chatInfo) {
NSLog(chatInfo);
};
Swift
A 类
1.声明
var onTap: (() -> ())?
2.实现
onTap?()
B 类
3.回调
xxx.onTap = {
print("回调")
}
Flutter
A 类
1.声明
VoidCallback onTap;
XXX({Key key, @required this.onTap}) : super(key: key);
2.调用
widget.onTap();
B 类
3.回调
XXX(onTap:(){
print("回调");
setState(() {});
},),
2.枚举
OC
typedef NS_ENUM(NSUInteger, JDStyle) {
JDStyleNormal,
JDStyleCancel,
};
@property(nonatomic, readonly, assign) JDStyle style;
Swift
用法一:
enum JDType {
case myGroup
case myAttention
var name: String {
switch self {
case .myGroup:
return "我的群组"
case .myAttention:
return "我的关注"
}
}
}
let types: [JDType] = [.myGroup, .myAttention,]
for (index , item) in types.enumerated() {
print("下标\(index) 值为\(item)")
}
用法二:
public enum RoleStatusType: Int {
case outsourced = 1
case regularEmployee
case trainee
case officialAccount
}
public var roleStatusType: RoleStatusType {
return RoleStatusType(rawValue: Int(empStatus)) ?? .regularEmployee
}
guard member.roleStatusType == .officialAccount else { return nil }
Flutter
enum Season {
spring,
summer,
autumn,
winter
}
void testDart(){
var currentSeason = Season.summer;
switch (currentSeason) {
case Season.spring:
print('1-3月');
break;
case Season.summer:
print('4-6月');
break;
case Season.autumn:
print('7-9月');
break;
case Season.winter:
print('10-12月');
break;
}
}
2.通知
OC
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"事件名称" object:nil];
- (void)notificationAction:(NSNotification *)notification{
Children *children = notification.object;
NSLog(@"触发通知");
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"事件名称" object:self];
- (void)dealloc {
1.移除指定的通知,不然会造成内存泄露
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"事件名称" object:nil];
2.下面的方法是可以移除Children中所有通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Swift
NotificationCenter.default.addObserver(self, selector: #selector(notificationAction), name: NSNotification.Name(rawValue:"事件名称"), object: nil)
@objc func notificationAction(nofi : Notification){
let str = nofi.userInfo!["content"]
print(String(describing: str!) + "this notifi")
}
NotificationCenter.default.post(name: NSNotification.Name("事件名称"), object: self, userInfo: ["content":"您好"])
deinit {
NotificationCenter.default.removeObserver(self)
}
Flutter
class EventBus {
/// 私有构造函数
EventBus._internal()
/// 保存单例
static EventBus _singleton = new EventBus._internal()
/// 工厂构造函数
factory EventBus() => _singleton
/// 保存事件订阅者队列,key:事件名(id),value: 对应事件的订阅者队列
var _eMap = new Map<Object, List<EventCallback>>()
/// 添加订阅者
void on(eventName, EventCallback f) {
if (eventName == null || f == null) return
_eMap[eventName] ??= new List<EventCallback>()
_eMap[eventName].add(f)
}
/// 移除订阅者
void off(eventName, [EventCallback f]) {
var list = _eMap[eventName]
if (eventName == null || list == null) return
if (f == null) {
_eMap[eventName] = null
} else {
list.remove(f)
}
}
/// 触发事件,事件触发后该事件所有订阅者会被调用
void send(eventName, [arg]) {
var list = _eMap[eventName]
if (list == null) return
int len = list.length - 1
//反向遍历,防止订阅者在回调中移除自身带来的下标错位
for (var i = len
list[i](arg)
}
}
}
EventBus().on("事件名称", (arg) {
});
EventBus().send("事件名称",arg);
void dispose() {
super.dispose();
EventBus().off("事件名称");
}