diff --git a/RuntimeDemo.xcodeproj/project.pbxproj b/RuntimeDemo.xcodeproj/project.pbxproj index b64fed7..d88460a 100644 --- a/RuntimeDemo.xcodeproj/project.pbxproj +++ b/RuntimeDemo.xcodeproj/project.pbxproj @@ -11,6 +11,9 @@ 691D6FC51C421E68003D41FB /* HYBPropertyLearn.m in Sources */ = {isa = PBXBuildFile; fileRef = 691D6FC41C421E68003D41FB /* HYBPropertyLearn.m */; }; 691D6FC61C421E68003D41FB /* HYBPropertyLearn.m in Sources */ = {isa = PBXBuildFile; fileRef = 691D6FC41C421E68003D41FB /* HYBPropertyLearn.m */; }; 691D6FC71C421E68003D41FB /* HYBPropertyLearn.m in Sources */ = {isa = PBXBuildFile; fileRef = 691D6FC41C421E68003D41FB /* HYBPropertyLearn.m */; }; + 69384B111C47631700B61526 /* HYBTestEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 69384B101C47631700B61526 /* HYBTestEntry.m */; }; + 69384B121C47631700B61526 /* HYBTestEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 69384B101C47631700B61526 /* HYBTestEntry.m */; }; + 69384B131C47631700B61526 /* HYBTestEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 69384B101C47631700B61526 /* HYBTestEntry.m */; }; 69654F5B1C2CDE6600FFB5AC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 69654F5A1C2CDE6600FFB5AC /* main.m */; }; 69654F5E1C2CDE6600FFB5AC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 69654F5D1C2CDE6600FFB5AC /* AppDelegate.m */; }; 69654F611C2CDE6600FFB5AC /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 69654F601C2CDE6600FFB5AC /* ViewController.m */; }; @@ -83,6 +86,8 @@ 54494B247380150E00DCBCF2 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = ""; }; 691D6FC31C421E68003D41FB /* HYBPropertyLearn.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HYBPropertyLearn.h; sourceTree = ""; }; 691D6FC41C421E68003D41FB /* HYBPropertyLearn.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HYBPropertyLearn.m; sourceTree = ""; }; + 69384B0F1C47631700B61526 /* HYBTestEntry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HYBTestEntry.h; sourceTree = ""; }; + 69384B101C47631700B61526 /* HYBTestEntry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HYBTestEntry.m; sourceTree = ""; }; 69654F561C2CDE6600FFB5AC /* RuntimeDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RuntimeDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 69654F5A1C2CDE6600FFB5AC /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 69654F5C1C2CDE6600FFB5AC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -171,6 +176,15 @@ name = Property; sourceTree = ""; }; + 69384B0E1C47630500B61526 /* TestEntry */ = { + isa = PBXGroup; + children = ( + 69384B0F1C47631700B61526 /* HYBTestEntry.h */, + 69384B101C47631700B61526 /* HYBTestEntry.m */, + ); + name = TestEntry; + sourceTree = ""; + }; 69654F4D1C2CDE6600FFB5AC = { isa = PBXGroup; children = ( @@ -196,6 +210,7 @@ 69654F581C2CDE6600FFB5AC /* RuntimeDemo */ = { isa = PBXGroup; children = ( + 69384B0E1C47630500B61526 /* TestEntry */, 69657DD21C44A66E0095C032 /* Method */, 691D6FC21C421E3E003D41FB /* Property */, 697364AE1C3A45F9003E1FE9 /* MethodExchange */, @@ -511,6 +526,7 @@ 69657DDA1C44D1A30095C032 /* NSMutableArray+Swizzling.m in Sources */, 698828201C3383F8002363D5 /* HDFArchiveModel.m in Sources */, 6980598D1C30D6070021085B /* HYBTestModel.m in Sources */, + 69384B111C47631700B61526 /* HYBTestEntry.m in Sources */, 698828351C34D476002363D5 /* HYBPig.m in Sources */, 69654F931C30214500FFB5AC /* UIControl+HYBBlock.m in Sources */, 69E0205C1C3771AF0034BF32 /* HYBMsgSend.m in Sources */, @@ -537,6 +553,7 @@ 698828311C34CBAB002363D5 /* HYBDog.m in Sources */, 6980598E1C30D6070021085B /* HYBTestModel.m in Sources */, 69657DE01C44D8620095C032 /* NSArray+Swizzling.m in Sources */, + 69384B121C47631700B61526 /* HYBTestEntry.m in Sources */, 69657DDB1C44D1A30095C032 /* NSMutableArray+Swizzling.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -559,6 +576,7 @@ 698828321C34CBAB002363D5 /* HYBDog.m in Sources */, 6980598F1C30D6070021085B /* HYBTestModel.m in Sources */, 69657DE11C44D8620095C032 /* NSArray+Swizzling.m in Sources */, + 69384B131C47631700B61526 /* HYBTestEntry.m in Sources */, 69657DDC1C44D1A30095C032 /* NSMutableArray+Swizzling.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -684,6 +702,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = RuntimeDemo/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.huangyibiao.RuntimeDemo; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -696,6 +715,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = RuntimeDemo/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.huangyibiao.RuntimeDemo; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/RuntimeDemo.xcworkspace/xcuserdata/huangyibiao.xcuserdatad/UserInterfaceState.xcuserstate b/RuntimeDemo.xcworkspace/xcuserdata/huangyibiao.xcuserdatad/UserInterfaceState.xcuserstate index f482349..fb8cef4 100644 Binary files a/RuntimeDemo.xcworkspace/xcuserdata/huangyibiao.xcuserdatad/UserInterfaceState.xcuserstate and b/RuntimeDemo.xcworkspace/xcuserdata/huangyibiao.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/RuntimeDemo/HYBCat.h b/RuntimeDemo/HYBCat.h index b572ff4..c669d40 100644 --- a/RuntimeDemo/HYBCat.h +++ b/RuntimeDemo/HYBCat.h @@ -11,7 +11,6 @@ @interface HYBCat : NSObject - + (void)test; @end diff --git a/RuntimeDemo/HYBMethodLearn.m b/RuntimeDemo/HYBMethodLearn.m index de7b495..fcaa52c 100644 --- a/RuntimeDemo/HYBMethodLearn.m +++ b/RuntimeDemo/HYBMethodLearn.m @@ -14,6 +14,7 @@ @implementation HYBMethodLearn - (int)testInstanceMethod:(NSString *)name andValue:(NSNumber *)value { NSLog(@"%@", name); + return value.intValue; } diff --git a/RuntimeDemo/HYBMsgSend.m b/RuntimeDemo/HYBMsgSend.m index 4144733..0bf2326 100644 --- a/RuntimeDemo/HYBMsgSend.m +++ b/RuntimeDemo/HYBMsgSend.m @@ -32,8 +32,11 @@ - (int)hasArguments:(NSString *)arg andReturnValue:(int)arg1 { } // C函数 -int cStyleFunc(const void *arg1, const void *arg2) { - NSLog(@"%s was called, arg1 is %s, and arg2 is %s", __FUNCTION__, arg1, arg2); +int cStyleFunc(id receiver, SEL sel, const void *arg1, const void *arg2) { + NSLog(@"%s was called, arg1 is %@, and arg2 is %@", + __FUNCTION__, + [NSString stringWithUTF8String:arg1], + [NSString stringWithUTF8String:arg1]); return 1; } @@ -70,9 +73,9 @@ + (void)test { @"参数1", 2016); NSLog(@"6. return value is %d", returnValue); - + NSLog(@"%s", @encode(const void *)); // 7.动态添加方法,然后调用C函数 - class_addMethod(msg.class, NSSelectorFromString(@"cStyleFunc"), (IMP)cStyleFunc, "i@:"); + class_addMethod(msg.class, NSSelectorFromString(@"cStyleFunc"), (IMP)cStyleFunc, "i@:r^vr^v"); returnValue = ((int (*)(id, SEL, const void *, const void *)) objc_msgSend)((id)msg, NSSelectorFromString(@"cStyleFunc"), @@ -81,15 +84,15 @@ + (void)test { NSLog(@"7. return value is %d", returnValue); // 8.返回浮点型时,调用objc_msgSend/objc_msgSend_fpret,其结果是一样的。 - float retFloatValue = ((float (*)(id, SEL))objc_msgSend_fpret)((id)msg, @selector(returnFloatType)); - NSLog(@"%f", retFloatValue); +// float retFloatValue = ((float (*)(id, SEL))objc_msgSend_fpret)((id)msg, @selector(returnFloatType)); +// NSLog(@"%f", retFloatValue); - retFloatValue = ((float (*)(id, SEL))objc_msgSend)((id)msg, @selector(returnFloatType)); - NSLog(@"%f", retFloatValue); +// retFloatValue = ((float (*)(id, SEL))objc_msgSend)((id)msg, @selector(returnFloatType)); +// NSLog(@"%f", retFloatValue); // 9.返回结构体时,不能使用objc_msgSend,而是要使用objc_msgSend_stret,否则会crash - CGRect frame = ((CGRect (*)(id, SEL))objc_msgSend_stret)((id)msg, @selector(returnTypeIsStruct)); - NSLog(@"9. return value is %@", NSStringFromCGRect(frame)); +// CGRect frame = ((CGRect (*)(id, SEL))objc_msgSend_stret)((id)msg, @selector(returnTypeIsStruct)); +// NSLog(@"9. return value is %@", NSStringFromCGRect(frame)); } diff --git a/RuntimeDemo/HYBPropertyLearn.m b/RuntimeDemo/HYBPropertyLearn.m index a911822..da5cb8e 100644 --- a/RuntimeDemo/HYBPropertyLearn.m +++ b/RuntimeDemo/HYBPropertyLearn.m @@ -53,6 +53,7 @@ - (void)getAllMemberVariables { } free(ivars); + } + (void)test { diff --git a/RuntimeDemo/HYBTestEntry.h b/RuntimeDemo/HYBTestEntry.h new file mode 100644 index 0000000..2d26434 --- /dev/null +++ b/RuntimeDemo/HYBTestEntry.h @@ -0,0 +1,15 @@ +// +// HYBTestEntry.h +// RuntimeDemo +// +// Created by huangyibiao on 16/1/14. +// Copyright © 2016年 huangyibiao. All rights reserved. +// + +#import + +@interface HYBTestEntry : NSObject + ++ (void)test; + +@end diff --git a/RuntimeDemo/HYBTestEntry.m b/RuntimeDemo/HYBTestEntry.m new file mode 100644 index 0000000..6e5a522 --- /dev/null +++ b/RuntimeDemo/HYBTestEntry.m @@ -0,0 +1,61 @@ +// +// HYBTestEntry.m +// RuntimeDemo +// +// Created by huangyibiao on 16/1/14. +// Copyright © 2016年 huangyibiao. All rights reserved. +// + +#import "HYBTestEntry.h" + +@implementation HYBTestEntry + + ++ (void)test { + NSDictionary *originDict = @{@"k1": @"v1", + @"k2": @"v2", + @"k3": @"v3", + @"k4": @"v4", + @"k5": @"v5", + @"k6": @"v6", + @"k7": @"v7", + @"k8": @"v8", + @"k9": @"v9"}; + NSDictionary *paramters = @{@"kk1": @"", + @"kk2": @"vv2"}; + + NSDictionary *resutDict = @{@"k1": @"v1", + @"k2": @"v2", + @"k3": @"v3", + @"k4": @"v4", + @"k5": @"v5", + @"k6": @"v6", + @"k7": @"v7", + @"k8": @"v8", + @"k9": @"v9", + @"kk1": @"", + @"kk2":@"vv2"}; + + for (NSUInteger i = 0; i < 1000000; ++i) { + NSMutableDictionary *allParams = [[NSMutableDictionary alloc] initWithDictionary:paramters]; + [allParams addEntriesFromDictionary:originDict]; + + // 判断是否相等 + if (resutDict.count != allParams.count) { + NSLog(@"merge but it is not equal"); + continue; + } + + for (NSString *key in resutDict.allKeys) { + NSString *value1 = [resutDict objectForKey:key]; + NSString *value2 = [allParams objectForKey:key]; + + if (![value1 isEqualToString:value2]) { + NSLog(@"merge but it is not equal"); + continue; + } + } + } +} + +@end diff --git a/RuntimeDemo/NSArray+Swizzling.h b/RuntimeDemo/NSArray+Swizzling.h index d20eee9..bbc7f3f 100644 --- a/RuntimeDemo/NSArray+Swizzling.h +++ b/RuntimeDemo/NSArray+Swizzling.h @@ -10,6 +10,6 @@ @interface NSArray (Swizzling) - +@property (nonatomic, weak) id delegate; @end diff --git a/RuntimeDemo/NSArray+Swizzling.m b/RuntimeDemo/NSArray+Swizzling.m index 0565d66..8942d8d 100644 --- a/RuntimeDemo/NSArray+Swizzling.m +++ b/RuntimeDemo/NSArray+Swizzling.m @@ -12,13 +12,13 @@ @implementation NSArray (Swizzling) -+ (void)load { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [self swizzleSelector:@selector(lastObject) - withSwizzledSelector:@selector(hdf_lastObject)]; - }); -} +//+ (void)load { +// static dispatch_once_t onceToken; +// dispatch_once(&onceToken, ^{ +// [self swizzleSelector:@selector(lastObject) +// withSwizzledSelector:@selector(hdf_lastObject)]; +// }); +//} - (id)hdf_lastObject { if (self.count == 0) { @@ -30,5 +30,13 @@ - (id)hdf_lastObject { return [self hdf_lastObject]; } +- (id)delegate { + + return objc_getAssociatedObject(self, @"__delegate__key"); +} + +- (void)setDelegate:(id)delegate { + objc_setAssociatedObject(self, @"__delegate__key", delegate, OBJC_ASSOCIATION_ASSIGN); +} @end diff --git a/RuntimeDemo/NSMutableArray+Swizzling.m b/RuntimeDemo/NSMutableArray+Swizzling.m index c806659..d2eabb7 100644 --- a/RuntimeDemo/NSMutableArray+Swizzling.m +++ b/RuntimeDemo/NSMutableArray+Swizzling.m @@ -41,12 +41,6 @@ - (instancetype)hdf_initWithObjects:(const id _Nonnull __unsafe_unretained *)ob if (objects[i] == nil) { hasNilObject = YES; NSLog(@"%s object at index %lu is nil, it will be filtered", __FUNCTION__, i); - -//#if DEBUG -// // 如果可以对数组中为nil的元素信息打印出来,增加更容易读懂的日志信息,这对于我们改bug就好定位多了 -// NSString *errorMsg = [NSString stringWithFormat:@"数组元素不能为nil,其index为: %lu", i]; -// NSAssert(objects[i] != nil, errorMsg); -//#endif } } @@ -61,6 +55,7 @@ - (instancetype)hdf_initWithObjects:(const id _Nonnull __unsafe_unretained *)ob } } + NSLog(@"%@", [NSThread callStackSymbols]); return [self hdf_initWithObjects:newObjects count:index]; } diff --git a/RuntimeDemo/ViewController.m b/RuntimeDemo/ViewController.m index 90158e4..08956b7 100644 --- a/RuntimeDemo/ViewController.m +++ b/RuntimeDemo/ViewController.m @@ -17,8 +17,9 @@ #import "HYBPropertyLearn.h" #import "HYBMethodLearn.h" #import "UIViewController+Swizzling.h" +#import "HYBTestEntry.h" -@interface ViewController () +@interface ViewController () @property (nonatomic, strong) UIWebView *webView; @@ -33,25 +34,85 @@ - (void)viewDidLoad { // [HYBTestModel test]; // [HDFArchiveModel test]; // [HYBCat test]; -// [HYBMsgSend test]; + [HYBMsgSend test]; // [HYBMethodExchange test]; // [HYBPropertyLearn test]; // [HYBMethodLearn test]; - [self testWebview]; +// [self testWebview]; +// [HYBTestEntry test]; } - (void)testWebview { self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; self.webView.delegate = self; - NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.haodf.com/"]]; + NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://t.cn/RbQXJ6j"]]; [self.webView loadRequest:req]; + [self.view addSubview:self.webView]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"url: %@", request.URL.absoluteString); + NSLog(@"query: %@", request.URL.query); +// [ViewController filterWebView:webView withRequest:request]; return YES; } ++ (BOOL)filterWebView:(UIWebView *)webView withRequest:(NSURLRequest *)request { + // 未登录状态下,不用增加 +// if (kIsEmptyString([HDFCipher token])) { +// return YES; +// } + + NSString *url = request.URL.absoluteString; + + if ([url rangeOfString:@"userId"].location != NSNotFound ) { + return YES; + } + + if ([url isEqualToString:@"about:blank"]) { + return YES; + } + + + NSString *query = request.URL.query; + NSString *format = @"%@?userId=%@&token=%@"; + if (query.length != 0) { + format = @"%@&userId=%@&token=%@"; + } + +// if ([HDFCipher isDoctorApp]) { + url = [NSString stringWithFormat:format, + url, +@"xxxxxx", + @"sdhfodsfdf"]; +// } else if ([HDFCipher isPatientApp]) { +// url = [NSString stringWithFormat:format, +// url, +// [[NSClassFromString(@"HaodfUserManager") sharedManager] userIdString], +// [HDFCipher token]]; +// } + NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; + [webView loadRequest:req]; + + NSString *url1=@"http://unmi.cc?p1=%+&sd &p2=中文"; + NSLog(@"encode: %@", [[self class] encodeURL:url1]); + + return NO; +} + ++ (NSString *)encodeURL:(NSString *)url { + NSString *newString = + CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, + (CFStringRef)url, + NULL, + CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding))); + if (newString) { + return newString; + } + + return url; +} + @end