Skip to content

Commit

Permalink
更新个小bug
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyibiao authored and huangyibiao committed Feb 18, 2016
1 parent 79a1317 commit 8704275
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 29 deletions.
20 changes: 20 additions & 0 deletions RuntimeDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
691D6FC31C421E68003D41FB /* HYBPropertyLearn.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HYBPropertyLearn.h; sourceTree = "<group>"; };
691D6FC41C421E68003D41FB /* HYBPropertyLearn.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HYBPropertyLearn.m; sourceTree = "<group>"; };
69384B0F1C47631700B61526 /* HYBTestEntry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HYBTestEntry.h; sourceTree = "<group>"; };
69384B101C47631700B61526 /* HYBTestEntry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HYBTestEntry.m; sourceTree = "<group>"; };
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 = "<group>"; };
69654F5C1C2CDE6600FFB5AC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +176,15 @@
name = Property;
sourceTree = "<group>";
};
69384B0E1C47630500B61526 /* TestEntry */ = {
isa = PBXGroup;
children = (
69384B0F1C47631700B61526 /* HYBTestEntry.h */,
69384B101C47631700B61526 /* HYBTestEntry.m */,
);
name = TestEntry;
sourceTree = "<group>";
};
69654F4D1C2CDE6600FFB5AC = {
isa = PBXGroup;
children = (
Expand All @@ -196,6 +210,7 @@
69654F581C2CDE6600FFB5AC /* RuntimeDemo */ = {
isa = PBXGroup;
children = (
69384B0E1C47630500B61526 /* TestEntry */,
69657DD21C44A66E0095C032 /* Method */,
691D6FC21C421E3E003D41FB /* Property */,
697364AE1C3A45F9003E1FE9 /* MethodExchange */,
Expand Down Expand Up @@ -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 */,
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)";
Expand All @@ -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)";
Expand Down
Binary file not shown.
1 change: 0 additions & 1 deletion RuntimeDemo/HYBCat.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@interface HYBCat : NSObject



+ (void)test;

@end
1 change: 1 addition & 0 deletions RuntimeDemo/HYBMethodLearn.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @implementation HYBMethodLearn

- (int)testInstanceMethod:(NSString *)name andValue:(NSNumber *)value {
NSLog(@"%@", name);


return value.intValue;
}
Expand Down
23 changes: 13 additions & 10 deletions RuntimeDemo/HYBMsgSend.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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"),
Expand All @@ -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));

}

Expand Down
1 change: 1 addition & 0 deletions RuntimeDemo/HYBPropertyLearn.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ - (void)getAllMemberVariables {
}

free(ivars);

}

+ (void)test {
Expand Down
15 changes: 15 additions & 0 deletions RuntimeDemo/HYBTestEntry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// HYBTestEntry.h
// RuntimeDemo
//
// Created by huangyibiao on 16/1/14.
// Copyright © 2016年 huangyibiao. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HYBTestEntry : NSObject

+ (void)test;

@end
61 changes: 61 additions & 0 deletions RuntimeDemo/HYBTestEntry.m
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion RuntimeDemo/NSArray+Swizzling.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

@interface NSArray (Swizzling)


@property (nonatomic, weak) id delegate;

@end
22 changes: 15 additions & 7 deletions RuntimeDemo/NSArray+Swizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
7 changes: 1 addition & 6 deletions RuntimeDemo/NSMutableArray+Swizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -61,6 +55,7 @@ - (instancetype)hdf_initWithObjects:(const id _Nonnull __unsafe_unretained *)ob
}
}

NSLog(@"%@", [NSThread callStackSymbols]);
return [self hdf_initWithObjects:newObjects count:index];
}

Expand Down
69 changes: 65 additions & 4 deletions RuntimeDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
#import "HYBPropertyLearn.h"
#import "HYBMethodLearn.h"
#import "UIViewController+Swizzling.h"
#import "HYBTestEntry.h"

@interface ViewController () <UIWebViewDelegate>
@interface ViewController () <UIWebViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIWebView *webView;

Expand All @@ -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

0 comments on commit 8704275

Please sign in to comment.