Objective, object

Source: Internet
Author: User

Objective, object

1. the category is the internal class of the system or the method for adding classes without source code. No instance variables are added.

2. The added method will become part of the original class, And the subclass can still be used.

3. The object name of the class is the original class name + Object Name

4. You can add instance methods or class methods.

 

Under the Xcode project;

1 command + N New

2. Select Objectiove-C File.

3 File: Set the name of the new Class; File Type: Category (select) Class: (select the name of the Class to be created)

// Some Date settings:

// NSDate indicates the date in the Foundation framework.

// Obtain the current time. The printed information includes: year, month, day, hour, minute, and second, and time zone. Zero Time Zone

NSDate * nowDate = [NSDate date];

NSLog (@ "nowDate is % @", nowDate );

// Obtain the date of tomorrow

NSDate * tomorrow = [NSDate dateWithTimeInterval: 24*60*60 sinceDate: nowDate];

NSLog (@ "tomorrow is % @", tomorrow );

 

NSTimeInterval seconds = [nowDate timeIntervalSinceDate: tomorrow];

NSLog (@ "%. 2f", seconds); // number of seconds in a year

NSTimeInterval time = [nowDate timeIntervalSince1970];

NSLog (@ "%. 2f", time );

// NSDateComponentsFormatter creation date format class, used to convert NSDate objects and NSString objects

// 1 create an object in Date Format

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

// 2 set the conversion format

/*

1 y years

2 M months

3 d

4 H 24 h (am/pm 12 hours)

5 MB

6 s

*/

[DateFormatter setDateFormat: @ "yyyy-mm-dd HH: mm: ss"];

// Use a date format object to complete conversion

// 1 convert a date object to a string object

NSString * str = [dateFormatter stringFromDate: nowDate]; // convert the date type to the encoding type

NSLog (@ "str is % @", str );

NSLog (@ "---- 1 ");

// 2 convert a string to an object

NSDate * date = [dateFormatter dateFromString: @ "11:11:33"];

NSLog (@ "date is % @", date );

 

// This method converts data of the date type to the string type

+ (NSString *) stringWithDate :( NSDate *) date formatter :( NSString *) formatter {

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[DateFormatter setDateFormat: formatter]; // you can specify the date format.

NSString * string = [dateFormatter stringFromDate: date]; // convert the date type to the encoding type

Return string;

}

// This method converts string data to date data.

+ (NSDate *) dateWithDateStringT :( NSString *) string {

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // required step creation format

NSDate * date = [dateFormatter dateFromString: string];

Return date;

}

 

// The string is converted to a date, and the format of the string can be flexibly used for external writing.

+ (NSDate *) dateWithDateString :( NSString *) string formatter :( NSString *) formatter {// Class Method

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[DateFormatter setDateFormat: formatter]; // required steps .. flexible use

NSDate * date = [dateFormatter dateFromString: string];

Return date;

}

 

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.