1. NSString constant definition:


     NSString * const name = @"value";
    Copy the code


     const NSString * name = @"value"; 
     NSString const * name = @"value";
    Copy the code

    Summary: Const before * means name is immutable, and const after * means name is immutable. We usually want to keep the value of name constant, so we should put const after *;

  2. Format the date to output different representations of the week:

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"E EE EEE EEEE EEEEE EEEEEE EEEEEEE"; NSString * dateStr = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"%@", dateStr); // Output: Saturday, Saturday, Saturday, Saturday, Saturday, Saturday, SaturdayCopy the code
  3. UICollectionView update data source and update interface must be one-to-one.

  4. URL encoding (memo, empty fill note):


    str = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)str,NULL,CFSTR("%+ /? #&="),kCFStringEncodingUTF8));Copy the code

    OC method:

     urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    Copy the code

    OC New method:

     - (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters NS_AVAILABLE(10_9, 7_0);
    Copy the code
  5. Different styles of UITableView:

    After iOS7, different uitableviewstyles look basically the same, so what are the subtle differences between them?

    • UITableViewStylePlain :

      1. SectionHeaderFooterThe default height is 0;
      2. andSectionThe headfoot view ofWill hoverAt the top and bottom;
    • UITableViewStyleGrouped :

      1. On the contrary,SectionThe head and foot height must beCannot be zero0 is invalid, the system will default a height, if not necessary can be set to a small decimal;
      2. SectionThe headfoot view ofDon’t hoverAbove and below, it slides along with the table;
  6. Git can’t track filename capitalization changes.

    (For example: the image name changed the case, but Git did not submit the change, resulting in the next check out code running APP can not read the image)

    1. Make git case-sensitive:

      • The command line:

          git config core.ignorecase false
        Copy the code
      • Directly modify the config file in the. Git folder

          ignorecase = false
        Copy the code
    2. If there is a problem, delete the file first and then add it

  7. Characters that cause JSON not to parse properly:

    • After testing: all Unicode characters (UCS-2 standard 0-65536) were iterated and parsed in JSON. It was found that 34 characters failed to be parsed. The first 32 characters were control characters with ASCII codes 0-31, and the other two were “and \.

      (ASCII code: actual characters) ("0:"."1:"."2:"."3:"."4:"."5:"."6:"."7:\a"."8:\b"."9:\t"."10:\n"."11:\v"."12:\f"."13: \ r","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:","24:","25:","26:","27:","28:","29:","30:","31:","34: \""."92: \ \"
      Copy the code
    • Solution: Replace or remove these characters before parsing JSON:

      1. Using the looping replace to null approach, the performance cost is less than 0.01 seconds and most JSON is less than 0.001 seconds:

        /** * deleteSpecialCharacters */ +(NSString *)deleteSpecialCharacters:(NSString *) STR {// delete characters that affect JSON parsing: [ASCII code :0 to 31 and"\] 34 characters in total,"\ Generally can not be removed directly, need to be handled separately.for (int i = 0; i <= 31; i++) {
                NSString *value = [NSString stringWithFormat:@"%C",(unichar)i];
                str = [str stringByReplacingOccurrencesOfString:value withString:@""];
            return str;
        Copy the code
      2. Optimized the substitution of special characters, now 10 times more efficient than before, json string substitution is basically within 0.001 seconds:

        /** * deleteSpecialCharacters */ +(NSString *)deleteSpecialCharacters:(NSString *) STR {// this method is 5-10 times faster than the above method. Use nsstrings - componentsSeparatedByCharactersInSet: in the control character to split a string into an NSArray; And then an NSPredicate to get rid of the empty string; Finally, use NSArray - componentsJoinedByString: //[NSCharacterSet controlset] controlCharacterSet ASCII code (0 to 31 and 127) NSArray<NSString *> *components = [str componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]]; components = [components filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]];
            str = [components componentsJoinedByString:@""];
            return str;
        Copy the code
  8. int a; // A doesn’t have to be equal to 0

    // error int a; a++; Int a = 0; a++;Copy the code
  9. Taking the absolute value of an int, using abs() is risky (-2147483648)

    Int n log (@)"%i", INT_MIN); // Output -2147483648 NSLog(@"%i", INT_MAX); // We know that the range of int is -2147483648 to 2147483647 // then we use abs() to take the absolute value of -2147483648: NSLog(@)"%i",abs(-2147483648)); // Output -2147483648 with Xcode warning // why? Shouldn't the absolute value of 2147483648 be 2147483648? This is because 2147483648 exceeds the maximum value of int (2147483647), so the highest bit is truncated. Therefore, we cannot use abs() to take the absolute value of -2147483648. // We should use the LABS () function, which takes arguments and returns values of long NSLog(@)"%li",labs(-2147483648)); // Output 2147483648 // Ok, this is correct, of course, this is only the extreme case, the general case is still safe to use the abs() functionCopy the code