Seventh Lecture. Nsdate,category (classification), extension (extension), Protocol (Protocol), delegate (agent)

Source: Internet
Author: User

First, master the use of nsdate and NSDateFormatter nsdate is cocoa? The base class for working with dates and times, encapsulating a given
Time (including date, times, timezone). Learn about the creation of NSDate, how to calculate the current time, the time interval, and the interaction between the time date and the character.
1>. NSDate creation, local time calculation, time interval calculation, time-to-character inter-transferExercise 1: Calculate the time difference between the current and a fixed timeExercise Two: Convert a string to a Date object Second, master the definition and use of category (only for Class extension class method or instance method) 1. The main role is: Add a method to a class without source code2.methods added by category become part of the original class. To achieve the ability to extend a class    3.How to define Category:
A. Create a new file
B. Select the Objective-c category template
c. Fill in the class name and category name
D. (. h) File Add method declaration
E. (. m) file addition method implementation
F. Where to use the call

Implementation of 4.caCategory:

nsstring+sayhi.h File

#import

@interface NSString (Sayhi)

-(void) Hi;

@end

NSSTRING+SAYHI.M File

#import "Nsstring+sayhi.h"

@implementation NSString (Sayhi)

#pragma mark implementation of the Hi method

-(void) Hi

{

NSLog(@ " this is the Hi method created by category for NSString ");

}

@end

The difference between 5.category and sub-categories

Exercise One: Flip or Flashback output a string (note that category is just an extension class method that includes the corresponding. m and. h files) Exercise Two: The comprehensive use of category and NSDateuse the category to add a class method (dewithdatestring:) for the NSDate class to convert the following string to a NSDate object. Convert @ "20140402142850" to NSDatethird, grasp the definition and use of Extension
the main function of 1.Extension is to add a ' private ' method to the class.
when we design a class, some methods need to be exposed externally (we call it an interface), and some methods may only be used internally (for example, a small step in a method)
Extension's function is to help us manage these internal use methods ("Private" method)
2.Extension Features:
A. Extension's grammatical format is similar to category
B.It is equivalent to moving the category. h file into the. m file of the original class , and its implementation is done in the. m file of the original class .
c. Extension is for its own class and must have a class of source code the difference between 3.extension and category
define extension: Add extension to the person.m file:Iv. mastering The definition and use of Protocol 1.Protocol also known as the Protocol, is the common technology in IOS development
2. The agreement is a set of criteria (a declaration of a bunch of methods) and only (. h) Files
3. The method that is defined in the implementation protocol of the object that accepts the agreement4.Protocol Features:(just like signing a contract, follow the contract Execution response task)
A. The agreement is like a task list (or a sticky note) that says a bunch of things to deal with. The list to whom, who will go to complete the task set out on the list.
B. After the agreement is defined, classes need to be followed to comply with the Protocol and to implement the method within the protocol .
C. The Compliance Agreement is written on the < protocol name > after the parent class name of the (. h) file. The method in implementing the Protocol is to implement the method in the protocol in the (. m) file. the equivalent of adding a hillock to this class. Instances of this class can call these methods. Example: Student class protocol. Corresponding studentProtocol.h student.h student.m file content codeStatement of the StudentProtocol.h Agreement (Agreement only. h file)Declaration of the Student.h file (mainly in compliance with the agreement)implementation of the STUDENT.M file (Implementation of protocol-related methods) < Span style= "font-weight:300;" > v. Mastering delegate

1.Protocol Core usage scenario is to implement delegate design pattern
2.delegate transliteration agent. Popular speaking is the agent, the main task is to help you complete a number of tasks
For example: A nanny can be considered a delegate, the main task is to help you bring children, cooking, laundry, etc.
3. Usage Scenarios:
When some tasks do not realize themselves, want to let others to achieve, you can designate an agent, let the agent help you to do. You just need to notify the agent to do something.

4. Proxy usage : First establish protocol (only. h file declares some methods)

Then let the two parties to the class agree to abide by the agreement respectively

again, let the protocol one side become a property of the other party's class, which is the key step in establishing the agent

Finally, the two sides of the Protocol method to implement the corresponding implementation, and then call to complete the agent event!

Example Analysis : the establishment of a nanny agreement, including children, cooking and other methods.

Comprehensive use of protocols and agents

NurseProtocol.h Agreement Statement

The Mather.h file follows the protocol, as well as the introduction of a proxy, which takes the nanny agent as a property of itself (the proxy flag) as a key step in introducing the agent

MATHER.M file implementation of the Agreement

Nurse.h file Introduction protocol and follow Word protocol

NURSE.M Implementing protocol-related methods

Seventh Lecture. Nsdate,category (classification), extension (extension), Protocol (Protocol), delegate (agent)

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.