oc--single Case--nsdate--archive--json--plist--protocol

Source: Internet
Author: User

-Single Case--------

Definition: A class allows only one object to be created, and this object can only be used whenever an object method or property is invoked anywhere. Note: In general, only limited class methods are created in the Singleton class.

Function: 1. A class can have only one object, and access it from a well-known access point 2. Save value

1. Set the objects of this class to static so that the object's uniqueness is guaranteed.

The Manage is a global

static Setupmanage *manage = nil;

+ (Setupmanage *) sharedmanage{

@synchronized (self) {//2 sync settings to ensure thread safety

3. If the object is empty, create an object

if (manage = = nil) {

manage = [[Setupmanage alloc]init];

}

}

4. Return the Created object

return manage;

}

Strictly speaking, we also want to rewrite the Allocwithzone: method when we call Alloc, the internal actually calls the Allocwithzone: method

+ (ID) Allocwithzone: (Nszone *) zone{

if (manage = = nil) {

manage = [Super Allocwithzone:zone];

}

return manage;

}

Releasing an object of a singleton

-(void) freemanage{

if (Manage! = nil) {

Manage = nil;

}

}

----------nsdate-----------

Gets the current system time direct output time GMT (GMT)

NSDate *date = [NSDate Date];

Output local time

(Nstimeinterval) is measured in seconds

NSDate *nowdate = [date datebyaddingtimeinterval:60*60*8];

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

Some time in the future 4001-01-01 00:00:00

NSDate *future = [NSDate distantfuture];

Some time in the past 0001-12-30 00:00:00

NSDate *past = [NSDate distantpast];

NSLog (@ "%@,%@", future,past);

Time comparison

NSDate *date1 = [NSDate Date];

if ([date isequaltodate:date1]) {NSLog (@ "Time");        }else{NSLog (@ "Time is different"); }

-------NSDateFormatter: A time converter, which converts a string to a nsdate type of data in a specified format as standard

Create an object of type NSDateFormatter

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

@ "1985-09-02 23:12:12"

Determine the standard

HH means 24-hour HH means 12-hour system

Formatter. DateFormat = @ "Yyyy-mm-dd HH:mm:ss";

NSDate *date = [NSDate Date];

Convert a date to a string

NSString *string = [Formatter stringfromdate:date];

String converted to date

NSDate *newdate = [Formatter datefromstring:string];

----------Archive------------

Archive: A way to store data, note: To implement archiving, you must comply with the Nscoding protocol

Divided into two types: 1. Archiving of classes of Systems 2. Archiving of custom Classes

----------json parsing-----------

NSString *string = [NSString stringwithcontentsoffile:@ "/users/desktop/shared Data/jsonuserlist.txt" encoding: Nsutf8stringencoding Error:nil];

NSLog (@ "%@", string);

Parsing with the JSON parser that comes with OC

First parameter: data to be parsed

Second parameter: (nsjsonreadingoptions)

Nsjsonreadingmutablecontainers = (1UL << 0), the top layer is a dictionary or an array

Nsjsonreadingmutableleaves = (1UL << 1), the top layer is a string

Nsjsonreadingallowfragments = (1UL << 2) The top layer cannot be a dictionary or an array

NSData *data = [string datausingencoding:nsutf8stringencoding];

Analytical

Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil];

NSLog (@ "dic%@", dic[@ "Count"]);

------------plist-------------

Plist: The nsdictionary,nsarray,nsnumber,bool,nsstring content is stored.

In most cases the outermost is the dictionary

Role: Store properties, attribute-level storage.

1. Locate the plist file and find the path

NSString *path = @ "/users/qianfeng/desktop/myinfo.plist";

2. Create an empty dictionary

Nsmutabledictionary *dictionary = [[Nsmutabledictionary alloc]init];

3. Add Data

[Dictionary setobject:[nsnumber Numberwithint:4] forkey:@ "age"];

[Dictionary setobject:[nsnumber Numberwithchar: ' C '] forkey:@ "symbol"];

[Dictionary setobject:@ "Lilei" forkey:@ "name"];

[Dictionary setobject:@[@ "Chinese", @ "中文版"] forkey:@ "score"];

4. Writing data to the plist file

The second argument: if you write halfway, something else happens, you can't continue, and the finished part is retained.

[Dictionary Writetofile:path Atomically:yes];

Read

Dictionary = [nsdictionary dictionarywithcontentsoffile:path];

------------Protocol----------------

1. What is the definition of the agreement?

A list of methods is called a protocol. Features: Only the declaration of the method is not implemented. Implementation put in: that class refers to this Protocol, implementation is placed in the implementation of this class

2. Format: @protocol protocol name

Declaration List of methods

@end

3. Use of the Agreement:

3.1 Creating an agreement 2. Introduction of Protocol 3. Reference protocol Note: You are to abide by this Agreement 4. The implementation of the method in the writing protocol

4. Classification of agreements: informal agreements and formal agreements

4.1 Informal agreements: declarations of classes, declarations of member variables, declarations of methods only

Formal agreement: An official name was given to the agreement.

Difference: Informal agreement: can only be referenced by itself and subclass, formal agreement: As long as this class references me, you can use my

5. Why use the agreement? (Using Engineering: Understanding Protocol)

Provides a unified interface. When multiple classes have the same function, we can extract this function to form a list of methods.

6. Classification of internal methods of formal agreements

6.1 The method that is modified by @optional is quoted, this class can write the implementation of the method, also can not write the method implementation

6.2 When a method modified by @required is referenced, the class must write the implementation of the method (the default is the required type

7. Agreement and inheritance

Inheritance: protocol

Same point: All can provide a unified interface

Different points: Creating a parent class for an existing class without creating a separate class

There can be a declaration of a member variable within a method declaration

Coupled high coupling is relatively lower than inheritance

A sibling relationship between classes and classes can be any class, provided that only

To comply with this agreement, you can

8. Other functions

A protocol can be referenced by multiple classes or protocols

A class or protocol can reference multiple protocols at the same time

The Declaration of a Protocol and class can be placed at the same time. In the file

------------Anonymous Object--------------

Dog *DOG1 = [[Dog alloc]init];//real name object: There is an object pointer pointing to this object

[[Dog alloc]init];// Anonymous object : An object that has no object pointer pointing to

oc--single Case--nsdate--archive--json--plist--protocol

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.