Skip to content

Instantly share code, notes, and snippets.

View 623637646's full-sized avatar
🎯
Focusing

Yanni Wang 王氩 623637646

🎯
Focusing
View GitHub Profile
@623637646
623637646 / CoreData.txt
Last active November 4, 2019 08:14
CoreData Object release 行为
Context不保存,NSManagedObject都不释放
NSManagedObject没有循环引用,只要Context保存,就释放
NSManagedObject存在循环引用,必须Context reset,才会释放
可以写个test case
@623637646
623637646 / verifyBRD.swift
Created October 11, 2019 08:39
Verify BRD's files.
//
// main.swift
// CheckBRD
//
// Created by Yanni Wang on 11/10/19.
// Copyright © 2019 Yanni. All rights reserved.
//
import Foundation
@623637646
623637646 / ThreadNotSafetyTests.m
Last active May 13, 2020 16:49
iOS multiple threads is not safety with nonatomic properties.
#import <XCTest/XCTest.h>
@interface ThreadNotSafetyTests : XCTestCase
// no crash
//@property (atomic) NSObject *obj;
// crash
@property (nonatomic) NSObject *obj;
@end
@623637646
623637646 / DoubleFloat+Deprecated.swift
Created September 5, 2019 08:28
Discard + - * / for Double and Float
import Foundation
// Double
func + (leftAddend: Double, rightAddend: Double) -> Double {
fatalError()
}
func - (leftAddend: Double, rightAddend: Double) -> Double {
fatalError()
}
@623637646
623637646 / AspectPositionInstead.m
Last active May 13, 2020 15:01
How to change the return value (for object) with Aspects's AspectPositionInstead. Refer to: https://www.jianshu.com/p/5ba69f35e034
@interface MyObject : NSObject
@property NSString *name;
@end
@implementation MyObject
-(void)dealloc
{
NSLog(@"dealloc");
}
@end
@623637646
623637646 / ReactiveObjCBug
Last active May 28, 2020 11:57
ReactiveObjC 3.0.0 bug. when hook NSString dealloc method, NSString can't release. Updated: This is not ReactiveObjC bug. Refer to: https://stackoverflow.com/q/62064040/9315497
int i = 0;
while (YES) {
@autoreleasepool {
NSString *string = [[NSString alloc] initWithFormat:@"%d", i];
// or
// NSString *string = [[NSString alloc] init];
[[string rac_willDeallocSignal] subscribeCompleted:^{
NSLog(@"obj dealloc");
}];
@623637646
623637646 / Associated.m
Last active April 24, 2019 02:54
Objective C weak associated
- (id)weakObject {
id (^block)() = objc_getAssociatedObject(self, @selector(weakObject));
return (block ? block() : nil);
}
- (void)setWeakObject:(id)object {
id __weak weakObject = object;
id (^block)() = ^{ return weakObject; };
objc_setAssociatedObject(self, @selector(weakObject),
block, OBJC_ASSOCIATION_COPY);
@623637646
623637646 / safari_memory_hole.html
Created March 14, 2019 08:53
Safari memory hole
<html>
<head>
<title>Safari Memory Hole</title>
</head>
<body>
<textarea>This file takes 30MB in Chrome but 2GB in Safari</textarea>
<script>
var m = {};
@623637646
623637646 / iOS push notification logic.txt
Last active April 24, 2019 02:54
iOS push notification logic
# iOS 10 or above
1. **Cold start**
1. normal notification
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
2. silent notification
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
2. **Hot start**
1. normal notification
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
@623637646
623637646 / UIImageWithName
Last active November 28, 2018 08:32
Get UIImage by name in framework
#define UIImageWithName(IMAGE_NAME)\
(^UIImage *(NSString *imageName){\
NSAssert(imageName.length != 0, @"imageName is empty");\
if (imageName.length == 0) {\
return nil;\
}\
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:NSClassFromString(@"SHPUIModule")] pathForResource:@"SHPUIKit" ofType:@"bundle"]];\
NSAssert(bundle != nil, @"bundle is nil");\
if (bundle == nil) {\
return nil;\