Integrate a Flutter module into your iOS and Android project

For Flutter module

flutter create --template module my_flutter

For iOS

# Uncomment the next line to define a global platform for your project
platform :ios, '10.0' //for flutter module
flutter_application_path…



- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {    if (@available(iOS 13.0, *)) {  
NSString *token = [self stringFromDeviceToken:deviceToken];
NSLog(@"asdaa token: %@",token);
} else {
NSString *token = [[NSString stringWithFormat:@"%@", deviceToken] tokenString];
NSLog(@"asdaa token: %@",token);
}
}
- (NSString *)stringFromDeviceToken:(NSData *)deviceToken {
NSUInteger length = deviceToken.length;
if (length == 0) {
return nil;
}
const unsigned char *buffer = deviceToken.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(length * 2)];
for (int i = 0; i < length; ++i) {
[hexString appendFormat:@"%02x", buffer[i]];
}
return [hexString copy];
}
@implementation UIViewController (Utilities)
void Swizzle(Class c, SEL orig…

__weak WeakStrongDance_objcViewController *wself = self;/*
// 無weak-strong-dance狀況
self.completionHandler = ^(NSString *returnResult) {
if (wself != nil) { // 這行時mVC可能存在,通過檢查
wself.myObjcString = @"new"; // 可能已經是nil,KVO時造成crash
}
};
*/
// 實作weak-strong-dance
// 做一次強引用,將若引用的指針賦值給強引用的指針,(sself 強引用 wself 指向的物件)
//如此,指向對象的引用計數變成2,即使主線程中的self因為超出作用域被釋放了,該對象的引用計數仍為1,避免該對象被銷毀。
self.completionHandler = ^(NSString *returnResult) {
__strong __typeof (wself) sself = wself;
sself.myObjcString = @"new";
};
//Q:block 內部定義了sself,會不會因此強引用sself?//A:不會。block 只有在截獲外部變量時才會引用他,像上述在內部新建一個,則沒有任和問題。self.completionHandler = { [weak self] result in
if let `self` = self { // swift 的 weak strong dance
self.mySwiftString = ""
}
}
self.completionHandler = { [weak self] result in
if let `self` = self { // swift 的 weak strong dance
self.mySwiftString = ""
}
}

記憶體管理與效能


CommonMode :
1. kCFRunLoopDefaultMode: 預設Mode通常主線程在此Mode下運行
2. UITrackingRunLoopMode: UI追蹤,如 ScrollView 的觸摸滑動等等
3. kCFRunLoopCommonModes: 此mode用以標記上述兩個mode,嚴格來說不算是mode
PrivateMode:
1. UIInitializationRunLoopMode: 用於啟動APP,基本上APP啟動後就不在使用
2. GSEventReceiveRunLoopMode: 屬於GraphicsServices,像是音量鍵
var sparkTimer = Timer() {
didSet {
RunLoop.current.add(sparkTimer, forMode: .common)
}
}
var UITrackingTimer = Timer() {
didSet {
RunLoop.current.add(sparkTimer, forMode: .tracking)
}
}




Xcode 10.0 enum:

//Xcode 10
enum myEnum: Int {
case apple
case banana
}
print(myEnum.apple.hashValue) //6301819286870297653
print(myEnum.banana.rawValue) //0
print(myEnum.banana.hashValue) //4559753696474043376
print(myEnum.banana.rawValue) //1

Xcode 9.4.1 enum:

Xcode 9.4.1
enum myEnum: Int {
case apple
case banana
}
print(myEnum.apple.hashValue) //0
print(myEnum.banana.rawValue) //1
print(myEnum.apple.hashValue) //0
print(myEnum.banana.rawValue) //1

HUA YU TSENG

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store