JSBUtil.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Util.m
  3. // Created by 杜文 on 16/12/27.
  4. // Copyright © 2016年 杜文. All rights reserved.
  5. //
  6. #import "JSBUtil.h"
  7. #import <objc/runtime.h>
  8. #import "DWKWebView.h"
  9. @implementation JSBUtil
  10. + (NSString *)objToJsonString:(id)dict
  11. {
  12. NSString *jsonString = nil;
  13. NSError *error;
  14. if (![NSJSONSerialization isValidJSONObject:dict]) {
  15. return @"{}";
  16. }
  17. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
  18. if (! jsonData) {
  19. return @"{}";
  20. } else {
  21. jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  22. }
  23. return jsonString;
  24. }
  25. //get this class all method
  26. + (NSArray *)allMethodFromClass:(Class)class {
  27. NSMutableArray *methods = [NSMutableArray array];
  28. while (class) {
  29. unsigned int count = 0;
  30. Method *method = class_copyMethodList(class, &count);
  31. for (unsigned int i = 0; i < count; i++) {
  32. SEL name1 = method_getName(method[i]);
  33. const char *selName= sel_getName(name1);
  34. NSString *strName = [NSString stringWithCString:selName encoding:NSUTF8StringEncoding];
  35. [methods addObject:strName];
  36. }
  37. free(method);
  38. Class cls = class_getSuperclass(class);
  39. class = [NSStringFromClass(cls) isEqualToString:NSStringFromClass([NSObject class])] ? nil : cls;
  40. }
  41. return [NSArray arrayWithArray:methods];
  42. }
  43. //return method name for xxx: or xxx:handle:
  44. +(NSString *)methodByNameArg:(NSInteger)argNum selName:(NSString *)selName class:(Class)class
  45. {
  46. NSString *result = nil;
  47. if(class){
  48. NSArray *arr = [JSBUtil allMethodFromClass:class];
  49. for (int i=0; i<arr.count; i++) {
  50. NSString *method = arr[i];
  51. NSArray *tmpArr = [method componentsSeparatedByString:@":"];
  52. NSRange range = [method rangeOfString:@":"];
  53. if (range.length > 0) {
  54. NSString *methodName = [method substringWithRange:NSMakeRange(0, range.location)];
  55. if ([methodName isEqualToString:selName] && tmpArr.count == (argNum + 1)) {
  56. result = method;
  57. return result;
  58. }
  59. }
  60. }
  61. }
  62. return result;
  63. }
  64. + (NSArray *)parseNamespace: (NSString *) method{
  65. NSRange range=[method rangeOfString:@"." options:NSBackwardsSearch];
  66. NSString *namespace=@"";
  67. if(range.location!=NSNotFound){
  68. namespace=[method substringToIndex:range.location];
  69. method=[method substringFromIndex:range.location+1];
  70. }
  71. return @[namespace,method];
  72. }
  73. + (id )jsonStringToObject:(NSString *)jsonString
  74. {
  75. if (jsonString == nil) {
  76. return nil;
  77. }
  78. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  79. NSError *err;
  80. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  81. options:NSJSONReadingMutableContainers
  82. error:&err];
  83. if(err)
  84. {
  85. NSLog(@"json解析失败:%@",err);
  86. return nil;
  87. }
  88. return dic;
  89. }
  90. @end