Common classes under the Foundation framework: NSNumber, NSDate, NSCalendar, NSDateFormatter, NSNull, NSKeyedArchiver, nsnumbernsdate
======================================
Common classes under the Foundation framework
======================================
1. [NSNumber]
[Note] basic data types such as int, float, char, and double are used.
[Note] basic variable types inherited from C language (int, float, char, double, etc.) cannot be added to the oc-specific data structures such as arrays and dictionaries. It is inconvenient to use, and cannot be managed by adding categories and other oc proprietary syntaxes.
[Also] It can be considered that NSNumber is a class for converting basic data to object data.
[Note] NSNumber is a class. This class is used to convert the basic data type into a class of the object data type.
[Note] You can first save the basic data type to the nsnumber object, and then store the nsnumber into an array or dictionary.
[Note] What NSNumber can do can be replaced by NSString. Therefore, NSString is more commonly used.
2. [NSDate]
[Note] NSDate is a date (time) class.
[Note] the difference in time zone is 8 hours.
Limit 1 // create NSDate at the current time
NSDate * myDate = [NSDate date];
NSLog (@ "myDate = % @", myDate );
Listen 2 // 24 hours from now on
NSTimeInterval secondsPerDay = 24*60*60;
NSDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow: secondsPerDay];
NSLog (@ "myDate = % @", tomorrow );
3 // create date based on the existing date
NSTimeInterval secondsPerDay1 = 24*60*60;
NSDate * now = [NSDate date];
NSDate * yesterDay = [now addTimeInterval:-secondsPerDay1];
NSLog (@ "yesterDay = % @", yesterDay );
4.1.4 // Date of comparison
BOOL sameDate = [now is1_todate: yesterDay];
NSLog (@ "sameDate = % lu", sameDate );
Limit 4.1 // obtain an earlier date
NSDate * earlierDate = [yesterDay earlierDate: now];
NSLog (@ "earlierDate = % @", earlierDate );
Limit 4.2 // a later date
NSDate * laterDate = [yesterDay laterDate: now];
NSLog (@ "laterDate = % @", laterDate );
Iii. NSCalendar
// Create a date using the NSCALENDAR class
// You can use the NSCalendar class to create a specified date.
NSDateComponents * comp = [[NSDateComponents alloc] init];
[Comp setMonth: 06];
[Comp setDay: 01];
[Comp setyear: 2001];
[Comp setHour: 24];
NSCalendar * myCal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate * myDate1 = [myCal dateFromComponents: comp];
NSLog (@ "myDate1 =%@", myDate1 );
// Obtain the date from the existing date
NSCalendar * myCal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
Unsigned units = NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit;
NSDateComponents * comp1 = [myCal components: units fromDate: [NSDate date];
NSInteger month = [comp1 month];
NSInteger year = [comp1 year];
NSInteger day = [comp1 day];
Delta 4 [NSDateFormatter] format date class
Some formats of NSDateFormatter
// Instantiate an NSDateFormatter object
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
// Set the time format, which can be set as needed
[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];
// Use [NSDate date] to obtain the current system time
NSString * currentDateStr = [dateFormatter stringFromDate: [NSDate date];
// Output format: 13:22:13
NSLog (@ "% @", currentDateStr );
Custom format
// The formatter format should be noted here. If it is a lowercase "hh", the time will be changed to 12 hours or 24 hours. In upper case, the value is in the 24-hour format.
[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];
[DateFormatter setDateFormat: @ "MM dd, yyyy # EEEE"]; // EEEE indicates the day of the week and EEE indicates the day of the week.
[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];
[DateFormatter setDateFormat: @ "MMMMd, yyyy"]; // MMMM is a month of xx, and a d can save the value of 0 before 01
The output format is set by setDateStyle and setTimeStyle. the date and time formats defined respectively are optional.
Typedef enum {
NSDateFormatterNoStyle = kCFDateFormatterNoStyle,
Nsdateformatter1_style = kcfdateformatter1_style, // "11/23/37" or"
NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle, // \ "Nov 23,193 7 \"
NSDateFormatterLongStyle = kCFDateFormatterLongStyle, // \ "November 23,193 7" or "3:30:32 \"
NSDateFormatterFullStyle = kCFDateFormatterFullStyle // "Tuesday, rjl 12,195 2 AD" or "3:30:42 PST"
} NSDateFormatterStyle;
V. Date comparison
The following methods are available for comparison between dates:
-(BOOL) is1_todate :( NSDate *) otherDate;
YES is returned for comparison with otherDate.
-(NSDate *) earlierDate :( NSDate *) anotherDate;
Returns the date earlier than anotherDate.
-(NSDate *) laterDate :( NSDate *) anotherDate;
Returns the date later than anotherDate.
-(NSComparisonResult) compare :( NSDate *) other;
This method is called for sorting:
. NSOrderedSame is returned when the date value saved by the instance and anotherDate are the same.
. NSOrderedDescending is returned when the date value saved by the instance is later than anotherDate.
. NSOrderedAscending is returned when the date value saved by the instance is earlier than anotherDate.
6. NSDate to NSString Conversion
// Convert NSDate to NSString and extract the month
NSString * time = [[NSString alloc] initWithFormat: @ "% @", myDate1];
NSLog (@ "% @", time );
Nsange range = {5, 2 };
NSLog (@ "month: % @", [time substringWithRange: range]);
Delta 7.IOS-NSDateDifference8Hours
// Method 1
-(Void) tDate
{
NSDate * date = [NSDatedate];
NSTimeZone * zone = [NSTimeZonesystemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate * localeDate = [date dateByAddingTimeInterval: interval];
NSLog (@ "% @", localeDate );
}
// Method 2
+ (NSString *) fixStringForDate :( NSDate *) date
{
NSDateFormatter * dateFormatter = [[NSDateFormatteralloc] init];
[DateFormatter setDateStyle: kCFDateFormatterFullStyle];
[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];
NSString * fixString = [dateFormatter stringFromDate: date];
[DateFormatter release];
Return fixString;
}
△△[ NSNull]
[Note] There are four empty things
[NULL] [nil] [Nil] [NSNull]
NULL: indicates that the base type pointer is NULL.
Int * p = NULL;
Nil: indicates that the object pointer is null.
Id obj = nil;
Nil: indicates that the Class variable is empty.
Class class = Nil;
NSNull: Used as a null element in data structures such as array dictionaries.
// Unique method
[NSNull null]; creates an object that indicates null
8. Archive NSKeyedArchiver
// Creates an array and initializes some data.
NSArray * array = [[NSArray alloc] initWithObjects: @ "zhangsan", @ "lisi", @ "wanger", @ "xiaoming", nil];
// Specify the name and path of the file to be saved.
// NSHomeDirectory () is the home path of the current system
// StringByAppendingPathComponent: Add a file named file.
// The file type can be left empty. The file name and suffix are random.
NSString * filePath = [NSHomeDirectory () stringByAppendingPathComponent: @ "file.txt"];
NSLog (@ "archive file path: % @", filePath );
// The NSKeyedArchiver class is used for archiving.
// The returned value indicates whether the archive is successful.
BOOL isSuccess = [NSKeyedArchiver archiveRootObject: array toFile: filePath];
If (isSuccess = YES ){
NSLog (@ "file saved successfully ");
}
// [Note] files archived in this way are encrypted and cannot be opened.
// Archive
NSArray * Arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath];
NSLog (@ "The retrieved data is: % @", Arr );
[Read the official documentation]
[Note] It is recommended that you read the official Xcode documentation. The official documentation is very standard and there is no error.
// Disadvantage:
1. Poor readability. (Do not understand) (official documents are concise)
2. There are few examples, and there is little reference to the demo.
// Advantages
1. Rigorous knowledge.
2. You can find the answer in some special (difficult) official documents (provided that you spend time reading and searching)
[Solution to programming problems]
1. You can use Baidu first.
2. Go to Google. (Google Answers 99% English documents, which can be consulted with youdao dictionary)
3. Consult with colleagues.
4. Please refer to the official documentation.
5. See the youdao dictionary.