Thoughts on objective-C during winter vacation

Source: Internet
Author: User

I bought this book "Learn objective-C on Mac". At the beginning, I bought a 250 page that looks quite thin. After reading it, I found that there are many common languages, the new features are all similar to those in C ++ and Java, that is, a new name. After reading the new features, I feel that I have gained a lot of attention, although I have only touched on a bit of fur.

1. Knowledge of C Language

2. Understand how to use xcode

3. understand some New syntaxes and new features.

Bytes ---------------------------------------------------------------------------------------------

1. Formal and informal protocols (replaced by the official @ Optional Protocol) and file loading and storage

@ Required must be implemented

@ Optional selective implementation

 

Example 1.

@ Protocol nscopying // deep Replication

-(ID) copywithzone :( nszone *) zone;

@ End

 

Copy of nsarray is a light copy.

 

@ Protocol nscoding // encoding and decoding

-(Void) encodewithcoder :( nscoder *) ACO;

-(ID) initwithcoder :( nscoder *) decoder;

 

Nscoding can be used to encode any object as nsdata. Available [data writetofile: "tmp.txt" atomically: True] stored on hard disk

 

Example 1:

@ Interface A: nsobject <nscopying, nscoding> </P> <p >{</P> <p> nsstring * Name; </P> <p >}</P> <p> @ property (copy) nsstring * Name; </P> <p> @ end </P> <p> @ implementation a </P> <p> @ synthesize name; </P> <p>-(ID) copywithzone :( nszone *) Zone <br/>{</P> <p> A *; <br/> A = [[self class] allocwithzone: Zone] init]; <br/>. name = self. name; <br/> return a; <br/>}</P> <p>-(void) encodewithcoder :( nscoder *) ACO der <br/>{< br/> [ACO der codewithobject: Name forkey: @ "name"]; <br/>}< br/>-(ID) initwithcoder :( nscoder *) decoder <br/>{< br/> If (Self = [Super init]) <br/>{< br/> nsstring * n = [decoder decodeobjectforkey: @ "name"]; </P> <p >}< br/> return self; <br/>}< br/>

 

The nskeyedarchiver class is encoded as nsdata.

Nsdata * Data = [nskeyedarchiver archiveddatawithrootobject: object]; automatic call of encodewithcoder

 

A * A = [nskeyedunarchiver unarchiveobjectwithdata: Data]; // decode

Bytes ------------------------------------------------------------------------------------------------------------

Attribute list nsarray nsdictionary nsstring nsnumber nsdate nsdata

1. nsarray

+ (ID) arraywithobject:... ends with nil

-(Unsigned) count;

-(ID) objectatindex :( unsigned) index;

 

Nsmutablearray

+ (ID) arraywithcapacity :( INT );

-(Void) addobject :( nsobject );

-(Void) removeobjectatindex :( INT) index;

2. nsdictionary

+ (ID) dicitonarywithobjectsandkeys :( ID) firstobject, (nsstring *) firstkey ...;

-(ID) objectforkey :( ID) Key;

 

Nsmutabledictionary

+ (ID) dicitonarywithcapacity :( INT) N;

-(Void) setobject :( ID) object forkey :( ID) Key;

-(Void) removeobjectforkey :( ID) Key;

 

3. nsnumber

Encapsulation standard type

Nsnumber * A = [nsnumber numberwithint: 10];

4. nsdate

+ (Nsdate *) date;

+ (Nsdate *) datewithtimeintervalsincenow :( INT);

5. nsdata

+ (Nsdata *) datawithbytes :( nsobject) length :( INT );

-(INT) length;

-(Nsobject) bytes;

 

 

Bytes --------------------------------------------------------------------------------------------------------

Predicate:

In fact, a predicate can be replaced by a for or if clause in a simple way.

Nspredicate:

+ (Nspredicate) predicatewithformat :....

-(Nsarray *) filteredarrayusingpredicate: predicate;

Example:@ Interface A: nsobject <br/>{< br/> nsstring * Name; <br/>}< br/> @ property nsstring * Name; <br/> @ end <br/> @ implementation a <br/> @ synthesize name; <br/> @ end <br/> @ interface B; nsobject <br/>{< br/> nsmutablearray * A; <br/>}< br/> @ end <br/> @ implementation B <br/>-(void) adda (nsstring * n) <br/>{< br/> A * object; <br/> object. name = N; <br/>. add (object); <br/>}< br/> @ end <br/> int main () <br/>{< br/> B * bobject = [[B alloc] init]; <br/> bobject. adda ("A"); <br/> bobject. adda ("B"); <br/> bobject. adda ("C"); <br/> bobject. adda ("D"); <br/> nspredicate * predicate = [nspredicate predicatewithformat: @ "name = % @", "a"]; <br/> nsarray * result = [bobject filteredarrayusingpredicate: predicate]; </P> <p>}

 

A lot of operators are returned for the predicate (which can follow the C Operator)

For example, the class of the example is used.

Nspredicate * predicate = [nspredicate predicatewithformat: @ "(name = 'A') and (name = 'B')"];

 

Array Operator name between {min, max };

Name in {"A", "B "};

Use of self:

Nsarray * A = [nsarray arraywithobject: @ "A", "B", nil];

Nspredicate * predicate = [nspredicate predicatewithformat: @ self in "'A', 'B'"];

Nsarray * B = [A filteredarrayusingpredicate: predicate];

 

String OPERATOR:

Beginswith

Endswith

Contains

 

[C] case-insensitive

[D] Ignore duplicate notes

All [cd] are ignored.

Example: beginswith [c]

 

 

Like Operator

Like '* a' contains.

Like '?? A * 'has two characters in front

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.