Cocoa Touch (v): app operating mechanism Nsrunloop, KVC, KVO, Notification

Source: Internet
Author: User

Runloop Related base class definitions

1. Date Object NSDate, NSDateFormatter

An instance of NSDate represents a date in which a thread can convert NSDate objects and NSString objects with the help of an instance of NSDateFormatter.

//The Date method returns the current time (now)NSDate *date =[NSDate Date]; //now:11:12:40//date:11:12:50Date = [NSDate Datewithtimeintervalsincenow:Ten];//returns the time 10 seconds after the current time//starting from 1970-1-1 00:00:00Date = [NSDate dateWithTimeIntervalSince1970:Ten];//returns the time of 1970-1-1 00:00:00 time 10 seconds//randomly returns a relatively distant future time.Date =[NSDate distantfuture]; //randomly returns a relatively distant past time.Date =[NSDate Distantpast]; //returns the number of milliseconds that have passed since 1970-1-1Nstimeinterval interval =[Date timeIntervalSince1970]; //compare with other timesNSDate *date2 =[NSDate Date]; //back to the earlier time[Date Earlierdate:date2]; //back to the time of the comparison later[Date Laterdate:date2]; //get a two time difference[Date1 timeintervalsincedate date2]; NSDate*date =[NSDate Date]; //2013-04-07 11:14:45NSDateFormatter *formatter =[[NSDateFormatter alloc] init]; //hh is 24 binary, HH is 12 binaryFormatter.dateformat =@"YYYY-MM-DD HH:mm:ss"; //Formatter.locale = [[[Nslocale alloc] initwithlocaleidentifier:@ "ZH_CN"] autorelease]; NSString *string=[Formatter stringfromdate:date]; NSLog (@"%@",string); //return time of Green rulingNSDate *date2 = [Formatter datefromstring:@"2010-09-09 13:14:56"];

2, Timer Nstimer

You can use an instance of Nstimer to register a timed event source in a Nsrunloop instance and register the timer instance as the observer for the event. In other words, a timer and timing event are bound, and we will explain it in conjunction with Runloop below.

Event Loop Object Nsrunloop

1. Run Loop concept

The Nsrunloop class encapsulates the process by which a thread enters the event loop, and a Runloop instance represents the event loop of a thread.

There are two types of events that are received by the thread in the event loop: input source and timer source. Thread invoke convenience function [Nstimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:] While creating a Nstimer instance, The default mode defaults to register a timer source in the current thread 's run loop, and adds the newly created timer to the run loop as the observer for the event.

However, each thread does not immediately add it to the run loop when it creates the timer, just call [Nstimer timerWithTimeInterval:target:selector:userInfo:repeats:], and then using [Nsrunloop Currentrunloop] to get the corresponding event loop object and then invoking the [Runloop AddTimer] method, a timed event is registered, and the current thread will not terminate directly until the timer expires. Or you can call [[Nstimer alloc] InitWithFireDate:interval:target:selector:userInfo:repeats:] To specify the start time of a timer.

2, Timer life cycle control

Using the fire method and the Invalidate method in the timer to control a timer's declaration period, this is necessary for a recurring timer.

3. Run Loop mode type

KV Monitoring Mechanism

The Cocoa development Framework has a built-in notification mechanism that enables notification of each observer after data changes.

KVC Key value encoding

KVO Key-Value monitoring

In what scenario do you need KVC? The simplest scenario, if a control's properties are declared as @property (nonatomic,readonly) read-only, then the property can only be modified by KVC. For example, when we need to replace the original Tabbar in Uitabbarcontroller with a custom tabbar.

Cocoa Touch (v): app operating mechanism Nsrunloop, KVC, KVO, Notification

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.