JSBUtil.m 3.2 KB

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