OC Basic Knowledge Summary

Source: Internet
Author: User
Tags array sort

    Oc:objective-c, the difference between object-oriented C language//oc and C//1.oc is a superset of C, all the syntax of C language can be used in OC//2.oc is Object-oriented//3.oc is a runtime language//4.oc    Class Library Rich NSString *string = [[NSObject alloc] init];    String is nsstring type at compile time, NSObject type//object-oriented programming at runtime: OOP//object-oriented Programming core: Classes and Objects//object-oriented three major features: encapsulation, inheritance, polymorphism//Class and Object relationship:    1. A class is an object's type (abstract)//2. An object is an instance of a class//How to recognize a class and an object://Whether it is specific to something//How to define a class://1. Select Cocoa Touch class//2. Write the class name (Big Hump method) 3. Select the parent class NSObject//4. Automatically generate. h and. m files//5..h Write instance variables and method declarations;. Implementation of the M write method//6. Optimizations: Overriding the Description method//How to create an object://1. Introduce the header file//2. Open up memory Space alloc//3. Initialize NSObject *object = [[Nsobjec    T alloc] init]; Note: 1. Add *//2 to the object. Open up memory space and initialize to fit together//#include, #import, @class difference//1. #import相比于 # include, to prevent duplicate references//[email  Protected] Compared to #import, there is no real introduction of the header file, just tell the compiler this class, generally with the use of, in. h using the @class, in. m using the #import//class composition//1. From the file:. h and. m, the code in. H is written Between @interface and @end, the code in. M is between @implementation and @end//NOTE: The class name and file name do not have a direct relationship, and are consistent for ease of management//2. Functionally: Feature and Behavior, feature--instance variable, behavior- > Methods, instance ChangesWritten in. h, the declaration of the method is written in. h, the implementation of the method is written in the. m////instance variable visibility//1.public, public//2.protected, protected//3.private, Private//Note: modifier scope: To Next modifier, or to curly brace} end//class inner class external//public instance variable name Object--instance variable name//protected instance variable name cannot be accessed (using Set ter and getter)//private instance variable name cannot be accessed (using setter and getter)//Note: Private decorated instance variable, subclass cannot inherit//method classification//1. Depending on the invocation method: Class method (+) and real    Example method (-), the class method is called by the class, and the instance method is called by the object//Note: the instance variable//2 cannot be accessed internally by the class method. According to the number of parameters://a. No parameter//+/-(return value type) method name;    B. A parameter//+/-(return value type) method Name: (parameter type) parameter name;    Note: When the parameter is an object, the type is added *//c. Multiple parameter//+/-(return value type) method Name 1: (parameter type 1) Parameter name 1 Method Name 2: (parameter type 2) parameter name 2 ...; Note: 1. Each parameter corresponds to a colon//2. A colon is a part of the method name//3. Each paragraph of the method is named//4 by the small hump method. A space is added between each paragraph of the method//3. The declaration is made in. h//a. Public method//b. Private Method (No Write a declaration, or write a declaration in extension)//method call//[Class name class method name]//[object instance method name]//oc The nature of the invocation of the method: message mechanism, [message Receiver message]//special method (in NSString *name for example)//1.setter//-(void) SetName: (NSString *) name//2.getter//-(NSString *) name//3.init//-(ID) Init//4. Custom initialization method//-(ID) INITWIthname: (NSString *) name//5. Convenient constructor//+ (ID) personwithname: (NSString *) Name//oc inherited features//1. One-way inheritance: A parent class can have multiple subclasses, a     Subclasses can have only one parent class//2. Single root inheritance: All classes inherit from NSObject, NSObject is the base class//subclass can inherit the parent class://1. Instance variable//2 non-private. Non-proprietary methods//with inheritance, when solving problems If more than one class has the same characteristics and behavior, it will write these characteristics and behavior into the parent class, the subclass inside only write its own unique characteristics and behavior//oc Common data types//1.nsinteger, 64 bits under a long, not 64 is an int//2.CGF Loat, 64-bit under is double, non 64-bit under IS float//3.nsnumber, basic data type is converted to object//4.nsstring and nsmutablestring nsstring *str = @ "IPhone"    ; NSLog (@ "%d", str.retaincount);//overflow, 1//5.nsarray and Nsmutablearray//6.nsdictionary and Nsmutabledictionary//7.NS Set and Nsmutableset//8.nsdate//9.nsdateformatter//10.nstimeinterval, equivalent to double//11.nscomparisonresult// 12.NSRange//13.nstimezone//14.id, equivalent to void *, can refer to any object type//nsarray, nsdictionary, Nsset difference//nsarray, ordered, element is an object, accessed by subscript element//nsdictionary, unordered, element is Key-value form appears, key is unique, access value through key//nsset, unordered, element cannot repeat//block//essence is anonymous letter The grammar of several//block borrowed from the letterThe syntax of the number pointer//int maxValue (int a, int b) The type of//block: Int (^) (int, int)//use typedef to rename the block type typedef int (^blocktyp    e) (int, int);    Defines a block variable int (^blockname) (int, int);    Blocktype blockName1;    The implementation section of block blockname = ^ (int a, int b) {return a + B;    };    Complete block int (^blockname2) (int, int) = ^ (int a, int b) {return a + B;    };    Block calls int sum = blockName2 (2, 3);    Iterate through arrays/* for (< #initialization #>; < #condition #>; < #increment #>) {< #statements #>    }/*/* for (< #type *object#> in < #collection #>) {< #statements #>} *//array sort    Nsarray *array = [Nsarray arraywithobjects:@ "1", nil];    Nsarray *sortarray = [Array sortedarrayusingselector: @selector (compare:)];  [Array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {if ([obj1 intvalue] > [obj2 intvalue])        {return nsordereddescending; } else if ([Obj1 IntvAlue] = = [Obj2 intvalue]) {return nsorderedsame;        } else {return nsorderedascending;    }    }]; Syntax sugar//@ "", NSString//@12, NSNumber//@[@ "1", @ "2"], Nsarray//array[1], take the second element of the array//@{@ "1": @ "111", @ "2" : @ "222"}, Nsdictionary//dictionary[@ "1"], take key for @ "1" corresponding to the value values//class extension//1. Extend//a for known classes. Directly modify the//b.extension, expand     Show private methods and instance variables//2. Extensions to unknown classes//a. Inherit//b.category, add methods, but cannot add instance variable//3.protocol, define only the declaration of the method, all classes can abide by this protocol, and complete the implementation of the method    Design pattern: Agent or Delegate (Delegate) Man *man = [[Man alloc] init];    Girl *girl = [[Girl alloc] init];    Man.delegate = Girl;    [Man Cook];    [Man Wash]; attributes, setter and getter, dot syntax//properties://Declaration @property//implement @synthesize//property modifier//1. Access control group//atomic, Atomic Sex, setter and getter mutually exclusive, relatively safe, but inefficient, default value//nonatomic, non-atomic, high efficiency//Note: Properties are decorated with atomic, and are not allowed to rewrite getter and setter methods//2. Memory Management Group//A Ssign, used for basic data types (Special: Delegate property using assign), default value//retain, for object type//copy, for object type, and for compliance with <nscopying>//NOTE: If the attribute is modified with retain and copy, be sure to do release//3 again in Dealloc. Read and write group//readwrite, help you to generate setter and getter, default value//readonly, only    Into Getter//4. Method Rename Group//setter= method name//getter= method name//memory management operation//1.alloc, open memory space, reference count from 0 to 1//2.retain, reference count +1 3.release, reference count-1//4.dealloc, cannot be manually called when the reference count changes from 1 to 0 o'clock, the system calls, reclaims the memory space//5.autorelease, in the future a certain period of time reference count-1//6.copy, copy a A new object, must obey <nscopying>, complete copywithzone: Method implementation, reference count from 0 to 1//7.new, [nsobject new] equivalent [[NSObject alloc] init], reference count from 0 to 1 nsdictionary *dic = [Nsdictionary dictionary];

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OC Basic Knowledge Summary

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.