Friends will inevitably encounter problems during development. How did you solve them? Might as well share with everyone! If any of the questions in this article are helpful to you, then it makes sense to exist! Anyway, no matter how to encounter problems to solve the problem, while solving the problem is also to improve the development experience of the channel!
Since there is a problem, how should we solve it?
First of all, most people will search Baidu to solve problems, no exception, but Baidu will have a lot of misunderstanding, or even mislead people, at the same time, the efficiency of solving problems is not very high, if it is a technical problem, you can go to:
-
Google
Simple than Baidu search the answer is relatively more, more accurate and more efficient, it is recommended to use English search -
Stack Overflow
A program related TO IT technical question and answer site, simply in the work70%
Everything can be solved here
Search is a way to solve problems, and the remaining 30% of problems still need to be solved independently, so some people will ask whether there is a way to improve the independent problem solving? For example: accumulate crash information encountered, learn to view crash information, view official documentation skills, etc.) This article will not be detailed, if you have time to write a separate article to explore how to efficiently solve the crash problem!
2. Tips for debugging crash problems with breakpoints
- (1) If the current breakpoint points to memory, then there is a problem allocating memory during initialization
- (2) Add global breakpoint
Add a global breakpoint. PNG
The purpose of a global breakpoint is when it is created
abnormal
orcollapse
Be able to stayabnormal
orcollapse
Instead of jumping to the main function
Crash breakpoint. PNG
However, adding a global breakpoint will jump to the main function, as shown above, and 80% of the problems are caused by the storyboard or XIB dragline not being cleaned up in time:
Drag line does not clear.png
This Button link has been deleted from the code, but it is not cleaned up in time, so it will crash!
3.NSString uses stringWithFormat concatenation
- keep
2
A decimal pointNSString *string = [NSString stringWithFormat:@"%.2f",M_PI]; // The output is: 3.14 NSLog(@"%@", string);Copy the code
- with
0
Method of completionNSInteger count = 5; NSString *string = [NSString stringWithFormat:@"%02zd",count]; NSString *string = [NSString stringWithFormat:@"%02zd",count]; // 05 NSLog(@"%@", string);Copy the code
- There are special symbols in the string
%
What do I doNSInteger count = 50; //% is a special symbol. If you use % in NSString, you should write NSString *string = [NSString String WithFormat:@"%zd%%",count]; // the output is: 50% NSLog(@"%@", string);Copy the code
- There are special symbols in the string
"
What do I doNSInteger count = 50; NSString *string = [NSString stringWithFormat:@"%zd "",count]; NSString *string = [NSString stringWithFormat:@"%zd "",count]; // The output is: 50" NSLog(@"%@", string);Copy the code
4. Check whether the GIF/PNG image is the correct pose
First of all, let’s think about how you can determine if an image URL obtained from the Internet is a GIF, is that true? As follows:
/ / assume that this is a network access URL nsstrings * path = @ "http://pic3.nipic.com/20090709/2893198_075124038_2.gif"; NSString *extensionName = path.pathextension; If ([extensionName lowercaseString isEqualToString: @ "GIF"]) {/ / GIF images} else {/ / not gifs}Copy the code
What? Isn’t that how you judged it? Oh, I see. Did you use string intercepts to judge? Ha ha just kidding! The above judgment seems to be ok, but it is not rigorous. How do you know the true type of an image without knowing the extension of the image? You can determine the true type of the image by taking the first byte of the image data. Here’s how to do it:
- (NSString *)contentTypeForImageData:(NSData *) Data {uint8_t c; [data getBytes:&c length:1]; switch (c) { case 0xFF: return @"jpeg"; case 0x89: return @"png"; case 0x47: return @"gif"; case 0x49: case 0x4D: return @"tiff"; case 0x52: if ([data length] < 12) { return nil; } NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding]; if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) { return @"webp"; } return nil; } return nil; }Copy the code
In fact, the first byte of image data is fixed, the first byte of a type of image is its identifier, let’s call this method:
/ / assume that this is a network access URL nsstrings * path = @ "http://pic.rpgsky.net/images/2016/07/26/3508cde5f0d29243c7d2ecbd6b9a30f1.png"; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]]; / / call get photo extension nsstrings * string = [self contentTypeForImageData: data]; PNG NSLog(@"%@",string);Copy the code
5.Button forbids touching events in two ways
You should know that there are a lot of requirements that are not allowed to click on the Button, and let the user know that the Button is not allowed to click, so we should set it like this:
Button. enabled = NO;Copy the code
However, there is a requirement that neither clicking nor changing the Button color:
/ / hold button the original state, will not change color button. UserInteractionEnabled = NO;Copy the code
6. The pit I walked through with XIB
(1) If there is a control in the XIB that has been explicitly sized, and the output frame is correct, but the effect is not the same (for example, the size is larger), then the autoresizingMask attribute is usually used. The solutions are as follows:
- (void)awakeFromNib {self.autoresizingMask = UIViewAutoresizingNone; }Copy the code
(2) If your controller’s view is created using the XIB, the size of the view is incorrect when you get it. Here we need to go to [UIScreen mainScreen]. Bounds to get the size, but the storyboard size is correct!
7. What method do you use to round the corners?
First, do you set it like this:
/ / set the cornerRadius to self. IconImage image half width (round) the self. IconImage. Layer. The cornerRadius = 20; self.iconImage.layer.masksToBounds = YES;Copy the code
Or in xiB&Storyboard, click on the image you want rounded:
Xib or storyboard set rounded corners.png
After this, we recommend that you try not to set it this way, because using too many layers will cause the phenomenon of stagnation, especially rounded corners or shadows. If you set rounded corners, we usually use drawing to do it:
/ * * set the circular images (on classification using) * / - (UIImage *) cutCircleImage {UIGraphicsBeginImageContextWithOptions (self. The size, NO, 0.0); / / get the context CGContextRef CTR = UIGraphicsGetCurrentContext (); // Set the circle CGRect rect = CGRectMake(0, 0, self.size. Width, self.size. Height); CGContextAddEllipseInRect(ctr, rect); / / cutting CGContextClip (CTR); // draw the image [self drawInRect:rect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }Copy the code
This method is to set rounded images, very efficient, will not cause the phenomenon of lag, you should put this method in a separate category
8. How to use ## and @# in macros
-
To use ##, first we add a macro
#define LRWeakSelf(type) __weak typeof(type) weak##type = type;Copy the code
## is used to connect weak with the type value as shown in the following figure:
## 的使用.png
-
# means to add a double quotation mark to the identifier immediately after it.
-
@ #
To use, we add a normal macro:#define LRToast(STR) [NSString stringWithFormat:@"%@", STR] NSLog(@"%@",LRToast(@" tips "));Copy the code
Note that I am just defining a random macro as an example, the above code is normal use, let’s look at adding
@ #
How it works:#define LRToast(STR) [NSString stringWithFormat:@"%@",@# STR] NSLog(@"%@",LRToast);Copy the code
We can see that
LRToast(Warm tips);
withLRToast(@" warm reminder ");
Difference, that is to say@ #
Can replace@ ""
Then we will save trouble later development, do not have to add@ ""
!
9. Automatic layout of Autolayout tips
Use automatic layouts in storyboards or XIbs. If you have a lot of controls and a complex layout, you will get a lot of error warnings. This is a great way to avoid Autolayout phobia!
Autolayout.png
Read from top to bottom as shown above is the current state of the constraint First Item Relation Second Item Constant(plus 10) Multiplier
10. Rules for iterative App development version numbers
How are simple version numbers managed in iOS? First of all, the first version of our App is launched for the first time, for example, 1.0.0 is the first version number:
1. After launching, we suddenly found a serious Bug, so we need to fix the updated version. At this time, our version number is 1.0.1
2. If there is a new requirement and a new function is added on the basis of the original, then our version number becomes 1.1.0, and we need to clear the third digit to 0 to superimpose and modify the second digit
3. If the required functions of the App are greatly changed and the number of updates is very large, then our version number will be changed to 2.0.0. We need to superposition and modify the first digit, and empty the other digits to 0
Like a little partner please like it! If there is insufficient place, please help to correct and supplement in time, by the way talk about your proposal!