# Series :iOS development – Quickly locate and fix bugs

I wasn’t going to write about it,

Busy work yesterday, be free and at leisure time, around BBS, post bar, accidentally found someone send bug. www.jianshu.com/p/b51ead39c…

The above is fantastic… You can check it out.

Curious, I downloaded the source code and it’s clean, there’s no code except for the Sotryboard dragging a couple of controls, binding the present and dismiss events, there’s nothing else like this

So compile and run the project as he said, and indeed there is this small and partial bug recurrence

I wanted to fix the bug

  • Fixing bugs is the first step

First of all, do not manage the memory of what problem, doubt whether vc did not release? Add print to the VC dealloc


-(void)dealloc{
    NSLog(@"%s",__FUNCTION__);
}

Copy the code

The vc can be freed normally, but the textField is not freed. Exclude VC references

  • The second step is to disable the first key condition described in the bug description

The project runs normally,vc can be released, and textField can also be released. So it’s this property that’s causing the bug.

  • Step 3 Since you can solve the bug by turning off this attribute, the bug must be caused by this attribute, so consider whether you can use handwriting. Name these widgets in VC and set this property manually

Compile and run, the bug reappears, indicating that the property in iOS11 will indeed strong reference control, resulting in cannot be released,

  • Since the release problem caused by strong reference in step 4, then adjust the timing of reference can be adjusted. So what we can think of here is we can use a weak pointer or some other weak reference to break a strong reference so we can use a block to break a strong reference, because UITextField doesn’t have a block method associated with it, so there’s no point in adding a method. Abandon it The second idea is to use the UITextField proxy, because developers who have used the proxy know that the weak attribute has already broken the strong reference condition, we just need to set it in the appropriate proxy method and then add the proxy

Once the agent is done, find the right place to write the code you need

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == self.psdTextField) {
        textField.secureTextEntry = YES;
    } else {
        textField.secureTextEntry = NO;
    }
    return YES;
}
Copy the code

Compile and run again, and the bug is gone.

  • The fifth step to solve the bug in order to prevent the occurrence of other inexplicable bug, need to set some general Settings, such as interface release end edit, VC release before the release of proxy
- (IBAction)back:(id)sender {
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
Copy the code

So the whole process of fixing the bug is to remove the secureTextEntry property of the storyboard, to add it manually, to prevent strong references, to set it in the proxy, and let the system automatically break strong references for us.

Bug fixes demo github.com/spicyShrimp…

In fact this is a very simple bugs to fix the problem So the question it raises us as iOS development how to quickly locate the bug problem At the time of our development, we often appear all sorts of strange bugs, and is not able to locate himself, can’t find the train of thought, for bug locating this is actually a skill and experience

We all know the way, break point, line by line, print log, check if it is correct output… More experienced users will use the console to Po some property values… This is something that almost every developer uses every day.

However, it is not these that can completely solve the bug. Solving the bug is not just the occurrence of bugs, and the location of the bug can only be solved. Many bugs are left by the system ability, for example, the file index problem in XCode9 that I mentioned in my last blog has nothing to do with the code For example, the above mentioned bug, small and partial, completely impossible to locate the bug on the line , for example, last night I help users solve a bug response chain relationship, view hierarchy is correct, but is not respond to events, the net friend is considered a bug cause child controls itself, did not change the way of thinking, thinking about the layer structure, whether can respond to events, such as basic common bugs, (the final question is out of here, the layer window Questions, not to be repeated here)

So HERE I simply summarize some experience in solving bugs and share it with you. I hope you don’t have any bugs.

  1. Print log—- Log can be the simplest and most direct reflection of many things, such as the value of properties, such as the object class is correct…..
  2. Multi-add key breakpoints —- similar to log, every bug we have problems, most of them have a recurrence path, we need to analyze the recurrence path step by step, we need to add breakpoints for each key point, one by one to eliminate can be repaired
  3. Learn to use advanced methods such as exception breakpoints. When we are unable to identify some recurrence path, we can use exception breakpoints to discover where the problem occurred

    About use method, everybody can study on the net by oneself, can

  4. Learn to use the Po command —- to add breakpoints. When exceptions or interruptions occur, we can use the Po command to obtain information without logs.
  5. Learn how to use other tools. There are many tools that are not described here, such as instrument. Mastery of these tools can be very helpful in solving bugs
  6. Use crash logs — we can save crash logs locally, or use some third-party bug collection SDK for access, such as Bugly, which can locate bug errors to a certain extent and help us solve them
  7. Key factor analysis bug appeared, for the above still can’t locate the bug, we need to analysis the key factors from the bug, don’t miss any details, such as can’t find the so-and-so method, that is either no reference file, or you don’t add to the project, or no implementation method, either the compiler found no index path, etc. As for the bug problem above, the key point is a property that we should have thought of at the beginning of locating the bug, but many people ignore it
  8. Using some different ideas to break the bug path, this is a question of experience, a lot of bugs, use common mentality cannot be solved, privatization may be the system, may be time to get, may be other factors can’t control, experience is required at this time, how to change the thinking, change the inherent idea to bypass the bug, also need to know, you can Refer to my another blog — — — — — – (iOS11 navigation button location problem blog.csdn.net/spicyShrimp…). For many reasons that cannot be solved by normal thinking due to system limitations, we have to find a new way. As for how to do this, we can only say here that practice makes perfect
  9. More accumulation of knowledge, a lot of factors, is not necessarily a bug, often are the result of the developers not rigorous logic, so the merits of the algorithm, thinking clearly can help us to reduce a lot of bugs, but bug and don’t worry, solve once, will increase in a record, we just recorded, next time will soon resolved, on its own There are many ways to learn, such as forums, books, videos, communication and so on. None of us should be spared
  10. For every update to iOS or Xcode (or any other development tool or environment), if possible, adapt and learn new differences as early as possible, and think about the impact of certain API changes on our development, so as to prevent early

In fact, whether the rapid positioning of the bug, but also indirectly reflects your ability level hope that we will never have a bug in the development of….

Welcome to visit my blog series: iOS development – preface + outline blog.csdn.net/spicyShrimp…