• By the way, a commercial

IOS developer group 532084214 provides a platform for everyone to exchange technology and chat fart

  • This article focuses on four questions
  1. The load use
  2. Aop is faceted oriented programming
  3. NSNumber Or Int
  4. @() is 64-bit

##1 Lighten the load on the appDelegate

After a long time of study, you finally mastered iOS. You found a job in iOS development and vowed to start your coding career. The boss thinks highly of you and then tells you that I think your technology is very sophisticated From the software architecture to the page display to you 😨

At the end of the project, the boss told you that our APP should have a function to share in the moments of friends in the future. Otherwise, how could I show the excellence of my product And then you hear that Freimonitor is good, and you go to Freimonitor and you look at their documentation and they tell you that you’re going to write this thing in the AppDelegate didFinishLaunch method

[UMSocialData setAppKey:@"XX"]; [UMSocialWechatHandler setWXAppId:@"XXX" appSecret:@"XX" url:@""]; [UMSocialQQHandler setQQWithAppId:@"XXX" appKey:@"XXX" url:@""];Copy the code

And then a few days later the boss says we need to do statistics on my page and you’ve added another line of code to the AppDelegate didFinishLaunch

The needs are endless I need bug stats (FIR HUD) to remind the user of the rating system (iRate) to push (jPush pigeon). Your determination to package your code perfectly and write it beautifully has long been defeated by your boss’s demands. Don’t worry

I don’t know if you’ve used IQKeyBoardManage and iRate smart libraries

Daniel’s Readme wrote this

1) CODELESS, Zero Line Of Code

2) Automatically //

3) No More UIScrollView // No scrollView required

4) No More Subclasses // Do not need to inherit parent classes

5) No More Manual Work // No configuration is required

6) No More #imports // No imports needed

Well, it’s not magic, it’s just that if you use the + load method you know that this code is going to be called automatically when a class is loaded into the runtime and that’s how it’s going to be called automatically

Write a class that inherits from NSObject

#import <Foundation/Foundation.h>

@interface ThirdPartService : NSObject

@end



 #import "ThirdPartService.h"
 #import "UMSocial.h"
 #import "UMSocialWechatHandler.h"
 #import "UMSocialQQHandler.h"
 #import <MobClick.h>
 #import <FIR/FIR.h>

@implementation ThirdPartService
 + (void)load {
Copy the code

static dispatch_once_t onceToken; Dispatch_once (&onceToken, ^{fir hud [fir handleCrashWithKey:@”XX”]; // UMSocialData setAppKey:@”XX”]; // Hide uninstalled platforms [UMSocialConfig] hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]]; [UMSocialWechatHandler setWXAppId:@”XX” appSecret:@”XX” url:@””]; [UMSocialQQHandler setQQWithAppId:@”XX” appKey:@”XX” url:@””]; // TODO UM count [MobClick startWithAppkey:@””]; [MobClick setCrashReportEnabled:NO]; NSLog(@” Third party service registration completed “); }); } @end is similar to position and you can also write it like this

Modules and services are completely disassembled

But some services like APNS require LaunchOption and you can only write it in the appdDelegate but then you’ve removed a lot of code and you’re left with a few fixed ones and then you can modify the appDelegate and it’ll feel pretty clear


ViewController inheritance?

And then we plugged in the umO statistics and the basic thing about umO statistics is the PV of the statistics page

So if you’re new to the game, it’s so easy that I’ve opened up some vc (HomeViewController) and I’ve written this in my code

-(void)viewWillAppear:(BOOL)animated {
Copy the code

[super viewWillAppear:animated];

#ifndef DEBUG
Copy the code

[MobClick beginLogPageView:NSStringFromClass([self class])]; #endif } -(void)viewWillDisappear:(BOOL)animated {     [super viewWillDisappear:animated]; #ifndef DEBUG          [MobClick endLogPageView:NSStringFromClass([self class])]; #endif }

And then I might have dozens or even hundreds of pages in a project and I can’t do that for every show

We were smart enough to think about inheritance

As MyBaseViewController: UIViewController will do a thing like this Our project all inherit from UIViewController class to inherit from MyBaseViewController yet do you really think so We had dozens of controllers on a project and I had to change every one of them

This kind of repetitive work is one, it’s boring but it’s error prone and you’re copying and the replicator is missing a class and the important thing is that a lot of the classes in our project don’t inherit directly from UIViewController but maybe UITableViewController UICollectionViewContr0ller UINavigationController even not commonly used UISearchDisPlayController UIPopoverController UIPresentController does not suddenly feel so much ah 😨

It’s not a bad thing, but in the future, when you’re a big guy and you recruit a kid and you tell him that all of your classes have to inherit from all the parent classes that you’ve written yourself, beginners will always make mistakes and some classes forget to inherit and it’s very difficult to look up later and it’s a waste of time, so this design is not reasonable

  • Brother Cold will teach you the dark magic Method Swizzling again

What is this self baidu

Here is an English/Chinese translation from the NSHipster blogger and an article explaining portal practices at Runtime

Using method intersections we can intercept attractive methods and code in this way we get the idea of AOP

In the code

#import <UIKit/ uikit. h> @interface UIViewController (AOP) #warning Run time change the method to do some aspect programming such as statistics etc "UIViewController+AOP.h" #import <objc/runtime.h> #import <MobClick.h> @implementation UIViewController (AOP) + (void)load {Copy the code

static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         Class class = [self class];         // When swizzling a class method, use the following:         // Class class = object_getClass((id)self);         swizzleMethod(class, @selector(viewDidLoad), @selector(aop_viewDidLoad));         swizzleMethod(class, @selector(viewDidAppear:), @selector(aop_viewDidAppear:));         swizzleMethod(class, @selector(viewWillAppear:), @selector(aop_viewWillAppear:));         swizzleMethod(class, @selector(viewWillDisappear:), @selector(aop_viewWillDisappear:));     }); }

 void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector)   {
Copy the code

Method originalMethod = class_getInstanceMethod(class, originalSelector);     Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);     BOOL didAddMethod =     class_addMethod(class,                     originalSelector,                     method_getImplementation(swizzledMethod),                     method_getTypeEncoding(swizzledMethod));          if (didAddMethod) {         class_replaceMethod(class,                             swizzledSelector,                             method_getImplementation(originalMethod),                             method_getTypeEncoding(originalMethod));     } else {         method_exchangeImplementations(originalMethod, swizzledMethod);     } } – (void)aop_viewDidAppear:(BOOL)animated {     [self aop_viewDidAppear:animated];

}

 -(void)aop_viewWillAppear:(BOOL)animated {
Copy the code

[self aop_viewWillAppear:animated];

#ifndef DEBUG
Copy the code

[MobClick beginLogPageView:NSStringFromClass([self class])]; #endif } -(void)aop_viewWillDisappear:(BOOL)animated {     [self aop_viewWillDisappear:animated]; #ifndef DEBUG          [MobClick endLogPageView:NSStringFromClass([self class])]; #endif } – (void)aop_viewDidLoad {     [self aop_viewDidLoad];

if ([self isKindOfClass:[UINavigationController class]]) { UINavigationController *nav = (UINavigationController *)self; nav.navigationBar.translucent = NO; nav.navigationBar.barTintColor = GLOBAL_NAVIGATION_BAR_TIN_COLOR; nav.navigationBar.tintColor = [UIColor whiteColor]; NSDictionary *titleAtt = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; [[UINavigationBar appearance] setTitleTextAttributes:titleAtt]; [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; } // self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; } @end

A picture code for easy viewing

We take full advantage of dark magic to achieve the benefits of faceted programming

Source of ideas here casatwy.com/iosying-yon…

Black magic is not poison Follow a specification written code will not Crash as long as it helps us solve the problem is a good thing dark magic performance bottleneck? You can use Time Profiel to test black magic is not universal. For example, in the navigation controller, we need to encapsulate gestures and unified management of the back button on the left. These things are better inherited

Technology is a tool black cat, white cat, good cat to catch mice


Gorgeous dividing lineCopy the code

Network access parameters using primitive data types or objects

So let’s look at two methods

 + (void)getDataAtPageNo:(NSNumber *)pageNo PageSize:(NSNumber *)pageSize 
complete:(CompleteBlock)complete {
Copy the code

NSMutableDictionary *param = [NSMutableDictionary dictionary];     if (pageSize) {       [param setObject:pageSize forKey:@”pageSize”];     }     [param setObject:pageNo forKey:@”pageNo”]; // SendRequest } + (void)getData2AtPageNo:(long )pageNo PageSize:(long )pageSize complete:(CompleteBlock)complete {     NSMutableDictionary *param = [NSMutableDictionary dictionary];

[param setObject:@(pageSize) forKey:@”pageSize”];   [param setObject:@(pageNo) forKey:@”pageNo”];     // SendRequest }

There are two main ways to design a method for the request with parameters when accessing the network

  1. Use objects as arguments
  2. Use basic data types as arguments

In general it doesn’t make a big difference but the idea that He gives is Never there’s a basic data type, right

In general, developers might think that there’s no difference. Let me give you an example. Okay

When designing a page to display data: the logic on the page is to load the first page with a length of 10 pages by default (the students on the Server side are generally very friendly and the length of each page is 10 by default), but the default parameters written in the background will be overwritten. For example, the Server will spit 20 pieces of data after passing 20 pages

  1. In the first design scenario, it is possible to keep a PageNo PageSize in a controllerobjectThe pageSize object is not required to be nil if you don’t need it to be refreshed or loaded, so the param may not have that parameter to pass to the Server. The Server will give us back 20 pieces of data for a page
  2. In the second design scenario, it is possible to keep a PageNo pageSize in a controller as wellBasic data typesIs given to the corresponding method when accessing the network request. There are no special requirements and we don’t set a value for PageSize but the base data type in traditional programming languages like OC and C has a default value of0Even though we didn’t assign pageSize but by default the system gives us an initial value of 0 so when we pass it to the Server it overwrites the default pageSize=10 that the Server wrote so that we don’t get any errors and we don’t get any data back and it’s incredibly difficult to debug

Therefore, in the network access, The advice given by Cold brother is Never to appear the basic data type


What are the advantages of using NSNumbers over primitive data types? 64-bit adaptation issues are commonly used as parameters for network requests cached or displayed on the page

  1. For network request parameters, NSNumber is the best way because NSDictionary can only put objects
  2. Caches, whether cached to plist or KeyArchive, require objects, so NSNumber is also a good fit

We see that there’s nothing wrong with that but let’s switch the device down to the iPhone5S which is a 32-bit device, right

In fact, in printf and NSLog the %d %zd %f placeholder is very strict and if you don’t do the project you can have unexpected results

So if we get an NSNumber we don’t really know if it’s an int long unsigned int Bool it’s a bit risky to convert directly to a type but Clang actually gives us a nice Macro @()

NSNumber is not a simple class It is the realization of the kind of cluster resources in cocoa http://www.cocoachina.com/ios/20140109/7681.html http://www.cocoachina.com/ios/20150106/10848.html http://www.cocoachina.com/ios/20141218/10688.html


Finally, an advertisement will be published to the public account if the owner invites my article

** Add a welcome wechat scan code to follow **Copy the code