IOS Summary: Summary of various pitfalls in the project, and summary of ios summary pitfalls

Source: Internet
Author: User
Tags convert string to json string to json

IOS Summary: Summary of various pitfalls in the project, and summary of ios summary pitfalls
1. Convert string to JSON

For network requests, if the server returns a string, We Need To encapsulate a class by ourselves, convert the requested string into a json object, and store it in the model.

Note: If a string contains special escape characters (such as \ n and \ t), you must first process the string.

The sample code is as follows:

 

+ (NSDictionary *) dictionaryWithJsonString :( NSString *) jsonString {if (jsonString = nil) {return nil;} jsonString = [jsonString separator: @ "\ r" withString: @ ""]; jsonString = [jsonString separator: @ "\ n" withString: @ ""]; jsonString = [jsonString stringByReplacingOccurrencesOfString: @ "\ s" withString: @ ""]; jsonString = [jsonString separator: @ "\ t" withString: @ "]; jsonString = [jsonString stringByReplacingOccurrencesOfString: @" \ v "withString: @ ""]; jsonString = [jsonString separator: @ "\ f" withString: @ ""]; jsonString = [jsonString stringByReplacingOccurrencesOfString: @ "\ B" withString: @ ""]; jsonString = [jsonString separator: @ "\ a" withString: @ ""]; jsonString = [jsonString stringByReplacingOccurrencesOfString: @ "\ e" withString: @ ""]; NSData * jsonData = [jsonString dataUsingEncoding: encoding]; NSError * err; NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: jsonData options: Unknown error: & err]; if (err) {YSXLog (@ "json parsing failed: % @", err); return nil;} return dic ;}

 

Ii. Image stretching
UIImageView * rightImagV = [[UIImageView alloc] init]; UIImage * img = [UIImage imageNamed: @ "tu_text_Values"]; // source image UIEdgeInsets edge = UIEdgeInsetsMake (5, myScalWidth (100), 5, myScalWidth (30); // UIImageResizingModeStretch: stretch mode. Fill the image by stretching the area of the rectangle specified by UIEdgeInsets. // UIImageResizingModeTile: Tile mode, the image img = [img resizableImageWithCapInsets: edge resizingMode: UIImageResizingModeStretch]; rightImagV is filled by repeatedly displaying the specified rectangular area of UIEdgeInsets. image = img; [rightImagV sizeToFit]; rightImagV. width = myScalWidth (73) + scoreL. width + myScalWidth (20); rightImagV. x = SCREEN_WIDTH-myScalWidth (10)-rightImagV. width; rightImagV. centerY = CGRectGetMidY (proV. frame); [topView addSubview: rightImagV]; scoreL. x = myScalWidth (83); scoreL. centerY = rightImagV. height * 0.5; [rightImagV addSubview: scoreL];

 

3. Label text adaptive frame

Method 1

This method is recommended. This method can obtain the height and enable automatic line feed and line spacing settings.

UILabel * infoLab=[[UILabel alloc] init];//    infoLab.text=self.infoText;    infoLab.font=[UIFont systemFontOfSize:myScalFont(28)];    infoLab.textColor=RGB(102, 102, 102, 1);    infoLab.numberOfLines=0;    NSMutableAttributedString *infoStr = [HP_NString createAttributeStringWithText:self.infoText LineSpace:myScalHeight(22) andFont:infoLab.font andColor:infoLab.textColor];    infoLab.attributedText = infoStr;    CGSize infoSize = [HP_NString sizeOfText:self.infoText withFont:infoLab.font andSize:CGSizeMake(bgView.valueOfW-myScalWidth(22)*2, 1000) andLineSpace:myScalHeight(22) andColor:infoLab.textColor];    infoLab.width=infoSize.width;    infoLab.height=infoSize.height;    infoLab.x=typeLab.valueOfX;    infoLab.y=typeLab.valueOfBottomMargin+myScalHeight(24);[self.view addSubview:infoLab];

 

Method 2

CGFloat detailInfoLabelX = CGRectGetMidX (questImageView. frame); CGFloat detailInfoLabelW = detailInfoView. width-detailInfoLabelX * 2; UILabel * detailInfoLabel = [[UILabel alloc] init]; detailInfoLabel. numberOfLines = 0; detailInfoLabel. text = @ "La la la la "; detailInfoLabel. textColor = RGB (102,102,102, 1); detailInfoLabel. font = [UIFont systemFontOfSize: myScalFont (20)]; CGSize detailSize = [detailInfoLabel. text sizeWithFont: detailInfoLabel. font constrainedToSize: CGSizeMake (detailInfoLabelW, MAXFLOAT) lineBreakMode: NSLineBreakByCharWrapping]; detailInfoLabel. x = detailInfoLabelX; detailInfoLabel. y = 0; detailInfoLabel. width = detailSize. width; detailInfoLabel. height = detailSize. height; [detailInfoView addSubview: detailInfoLabel];

 

4. One day interval

Project requirements: control the number of pop-up windows. You only need to pop up once a day to write a class for convenient calling.

+ (Void) jumpToVC :( UIViewController *) myVC preview :( NSString *) saveParam withSaveDate :( NSDate *) saveDate withNavigationController :( UINavigationController *) nav {// determine whether the parameter is saved if. length> 0 & saveParam! = Nil) {// Y YSXLog (@ "parameter saved");} else {// N // determine whether the time is saved if (saveDate! = Nil) {// Y // determine whether the value exceeds 24 hours. if ([[NSDate date] timeIntervalSinceDate: saveDate]/3600> 24) {// exceeds 24 hours [nav pushViewController: myVC animated: YES];} else {YSXLog (@ "no more than 24 hours");} else {// N jump to [nav pushViewController: myVC animated: YES] ;}}

During the call, because the "depended interface" is not fully loaded, sometimes the pop-up fails. You can delay the pop-up time by 1 second.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    EmailViewController * vc = [[EmailViewController alloc] init];    [YSXJumpToVC jumpToVC:vc withSaveParam:[YSXUserInfo sharedYSXUserInfo].addEmail withSaveDate:[YSXUserInfo sharedYSXUserInfo].addEmailDate withNavigationController:self.navigationController];});

 

V. Comparison of Two dates

The slave server returns two times in the form of strings, and the size of the two must be compared.

NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; NSDate * dt1 = [[NSDate alloc] init]; NSDate * dt2 = [[NSDate alloc] init]; // dt1 = [df dateFromString: [YSXUserInfo sharedYSXUserInfo]. comentTimeStr1]; // real-time acquisition time dt2 = [df dateFromString: [YSXUserInfo sharedYSXUserInfo]. comentTimeStr]; NSComparisonResult result = [dt1 compare: dt2]; if (result = NSOrderedDescending) {// dt1> dt2 redView. hidden = NO;} else {redView. hidden = YES;} // when dt1 is greater than dt2, the result is NSOrderedDescending // when dt1 is equal to dt2, the result is NSOrderedSame // when dt1 is less than dt2, the result is NSOrderedAscending

 

6. Invalid shadow effect added to UIView

Add a shadow effect around the rounded view. The result was not obtained after half a day. It turned out that when I rounded the view, except for the View. layer. the setting of cornerRadius always adds View. layer. masksToBounds = YES. Of course no shadow is cropped.

7. Hide the status bar

Generally, when we create an interface, the system reserves 20 PX blank space for the top status bar, but this blank space is not easy to see, so we add self to the viewDidLoad method in the corresponding controller. automaticallyAdjustsScrollViewInsets = NO, and [[UIApplication sharedApplication] setStatusBarHidden: YES
WithAnimation: UIStatusBarAnimationFade]; it hides the entire Status Bar and the user experience is poor. When I processed the status bar, I found that the automaticallyAdjustsScrollViewInsets method did not work. After online query, the final solution was solved. In the original controller, I used scrollView as the first view, as long as the first view identity of the scrollView is canceled, the automaticallyAdjustsScrollViewInsets method works.

OK. Let's summarize these points today. I hope you can give me some advice!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.