Learn Objective notes over the past few days

Source: Internet
Author: User

1. The definition of a class is generally divided into two parts:
@ Interface
1. determine who the class inherits from and use the colon ":"
2. Confirm the data, including in "{}"
3. determine the actions that can be executed
-Instance method
+ Class methods
4. end with @ end

@ Implementation

2. The program has a GC mechanism, but it is best to manage the memory by yourself. The IPhone does not support GC either.
3. Generally, the get method name in OC is the same as the name of the corresponding property.
4. The modulo operation in OC only supports Integer Operation.
5. Enter the value through the keyboard:
Int num = 0;
NSLog (@ "Please input a number, type is integer !!! ");
Scanf ("% I", & num );
6. bool type in OC:
TRUE Value: Non-0 digits, true, TRUE, YES
FALSE values: 0, false, FALSE, or NO
7. @ implementation of the class generally does not repeatedly declare instance variables (although this can be done). When referenced elsewhere, header files will be referenced in the first class instead of the @ implementation file.
8. The difference between using <> and "" in import is: <> first, search for the system header file, and "" first searches for the file in the current directory.
9. @ property name 1, property name 2,...; declare the property setter and getter in the @ interface file
@ Synthesize attribute name 1, attribute name 2,...; implement the default setter and getter in the @ implementation file
The preceding commands only apply to basic types.
10. You can use "Instance name. attribute name" in OC to access attributes.
11. method declaration
Modifier (-/+) (return type or void) method name Part 1: (type) parameter name
Method Name Part 2: (type) parameter name
Method Name Part 3: (type) parameter name
......
{
Method body
}
All parts except the first part of the method name can be omitted. If the parameter type or return value of a method is a reference type, you must add *

Modifier (-/+) (return type or void) method name Part 1: (type) parameter name
(Type) parameter name
(Type) parameter name
......
{
Method body
}
12. If the local variable does not have an initial value, a value must be assigned during the declaration. The method parameter is a local variable, and the method cannot be modified.
13. Generally, the static variable is stored out of @ implementation, so that all methods and attributes in the class can access it.
14. The self keyword calls your own method or variable.
15. The Coordinate System Used in OC is the Cartesian coordinate, that is, the lower left point of a graph is the source point (0, 0) of the image ).
16. The @ class command can replace import "class name. h", which improves the efficiency, because it is only a method for the class that references this class;
If you want to reference methods in this class, you need to use import to know the methods and variables in this class.
17. Rewrite the dealloc method, release its own memory, and call [super release].
18. When creating an object, you must first determine whether it is nil. If it is not nil, you must first release the memory.
19. Polymorphism: Objects of different classes can share methods with the same name;
Dynamic type: the type of the object is not known until execution.
Dynamic binding: the object to which the method belongs is not known until execution.
20. methods for handling dynamic types:
-(BOOL) isKindOf: whether the class-object is a class-object or a member of its subclass
-(BOOL) isMemberOfClass: whether the class-object is a member of the class-object
-(BOOL) respondsToSelector: Can the selector object correspond to the method specified by the selector?
+ (BOOL) instancesRespondToSelector: whether the class instance specified by selector can respond to selector
+ (BOOL) isSubclassOfClass: is the class-object a subclass of a specified class?
-(Id) specify mselector: selector applies the method specified by selector.
-(Id) specify mselector: selector withObject: the object applies the method specified by selector, passing the object parameter
-(Id) specify mselector: selector widthObject: object1 widthObject: apply the method specified by selector to object2 and pass the parameters object1 and object2.
Get class-object: [class name class] or [object Name class]
Obtain selector: You can apply the @ selector command to a method name: @ selector (method name). Note that the colon in the method name ":"
The return value of selector is a SEL type.
21. Exception Handling: When an exception occurs, the program will not be terminated, but will jump to the catch for handling.
@ Try {
Statement;
Statement;
}
@ Catch (nsexception * exception ){
Statement;
Statement;
Nslog (@ "Name: % @, reason: % @", [Exception name], [Exception reason]);
}
@ Throw your own exception
You can use the @ finally statement block to determine whether to execute the @ try block that throws an exception.
22. Example of initialization method:
-(Person *) initwithname :( nsstring *) Name andcompany: (nsstring *) Company
{
Self = [Super init];
If (Self ){
[Self setname: @ "zhangsan" Company: @ "Lenovo"];
}
Return self;
}
23. Access scope of instance variables:
@ Protected: It can be referenced by any method of the current class and subclass. This is also the default situation.
@ PRIVATE: it can only be referenced by the method of the current class, And the subclass and other classes cannot be referenced.
@ Public: It can be referenced by methods of the current class, subclass, and other modules.
@ Package: For a 64-bit image, you can access this instance variable wherever the image of this class is implemented.
24. All methods, attributes, and variables defined in the source file of a class are called global variables and can be accessed by other modules and the module. For example, declare int NUMA = 1000 in Class;
When other classes use this global variable, declare this variable and add an extern keyword before it. For example, in Class B: extern int NUMA)
If the global variable is used in the Declaration class, you do not need to add an extern keyword.
25. Static variables are defined outside all functions, but cannot be accessed by other modules. They can only be used in the modules currently declared.
26. The auto keyword is opposite to the static keyword. It is the default declaration method for instance variables and instance methods, and nobody uses it.
27. const is used to modify a constant. Initialization is required during the declaration, and the value cannot be modified after the declaration. Otherwise, an error is reported.
28. The opposite of volatile and const indicates that the modified volume is a variable, avoiding the compiler's optimization of seemingly redundant variable assignment and avoiding repeated checks of variables without changing values.
29. typedef statement: type name defined by typedef type name.
30. Category:
# Import "class name. H"
@ Interface Class Name (category name)
{
}
# Improt "class name. H"
@ Implementation Class Name (category name)
{
}
Note: The instance variables cannot be newly defined, it is best not to use methods in the class, it must be consistent with the class name, or the method in the classification is not implemented.
31. Protocol: A list of methods shared by multiple classes. The methods in the Protocol are not implemented by others. If you decide to implement all the methods in the protocol, it means to comply with (confirm to) or adopt (adopt) this protocol.
Definition: @ protocol name
// @ Optional the method defined after does not need to be implemented
// The method defined after @ required must be implemented
@ End
Adopt one protocol:
@ Interface Class Name: Implementation class <protocol name 1, protocol name 2, protocol name 3,...>
 
@ End
Determine whether an object adopts a protocol:
If ([Object Name confirmsTo: @ protocol (protocol name)] = YES ){
// @ Protocol (Protocol name) Get the protocol name and return a Protocol object
}
Let the compiler help check whether a protocol is followed:
Id <protocol name> Object Name. If the specified protocol name is not followed, a warning is displayed.
Both protocols and classifications can comply with one protocol.
Informal protocols are actually a classification and can be used without implementing informal protocols. In Objective-C 2.0, @ optional is used to replace informal protocols.
32. Pre-processing commands:
# Define name value (execute text replacement)
Example: # define IS_LEAP_YEAR (y) y % 400 = 0 | (y % 4 = 0 | \
Y % 100! = 0)
33. NSInteger is not a class, but a typedef of the basic type.
34. NSArray can only store objects but not basic types.
35. Create an NSNumber object: NSNumber * numberInteger = [NSNumber numberWithInteger: 100];
36. There is an NSNumber to get an NSInteger: NSInteger numbetInt = [numberInteger integerValue];
37. numberWithXXX: Obtain the NSNumber object (class method)
InitWithXXX: Assign the initial value to the NSNumber object (instance method)
YYYValue: Obtain NSXXX data (instance method)
Among them, XXX includes: Integer, UnsignedInteger,
Int, UnsignedInt,
Short, UnsignedShort,
LongLong, UnsignedLongLong,
Long, UnsignedLong,
Char, UnsignedCha,
Float, Double, Bool
YYY: integer, unsignedInteger,
Int, unsignedInt,
Short, unsignedShort,
Longlong, unsignedLong,
Long, unsignedLong,
Char, unsignedChar,
Float, double, bool

38. [NSNumber object isw.tonumber: NSNumber object]: determines whether two numeric objects are equal
39. [NSNumber object compare: NSNumber object]: compare two objects with the size and return value type NSComparisonResult:
If the value is smaller than: NSOrderedAscending;
Greater than: NSOrderedDescending;
Equal to: NSOrderedSame;

40. Commonly Used @ "XXX" is the object of NSConstantString. This class is a subclass of NSString:
NSConstantString * str = @ "Hello World !!! ";
NSLog (str );
[Str release];
41.% @: NSLog (); print the object in the function.
42. NSString and NSConstantString both belong to immutable strings, while NSMutableString belongs to variable strings.
43. [str1 stringByAppendingString: str2]: append str2 to str1 and return the appended string.
[Str1 uppercaseString]: converts to uppercase and returns the converted string without changing the content of the original string.
[Str1 lowercaseString]: converts the string to lowercase and returns the converted string without changing the content of the original string.
[NSString stringWithString: str1]: returns a string using a string.
[Str1 compare: str2]: compare whether the content of two strings is equal, case sensitive!
[Str1 caseInsensitiveCompare: str2]: Compares two strings to determine if they are equal, case insensitive!
[Str1 substringFromString: 3]: truncate a string starting from 4th characters to the end of the string, and return the truncation result.
[Str1 substringToString: 7]: truncates the string from the first character to the eighth character, and returns the truncation result.
[Str3 substringWithRange: NSMakeRange ()]: truncates three strings from the second string

 
44. nsange Data Structure: two variables: location and length
Nsange range = [str3 rangeOfString: @ "Worl"]; // If not, the location = NSNotFound
NSLog (@ "range. location: % I, range. length: % I", range. location, range. length );

45. NSMutableString
NSMutableString * ms;
MS = [NSMutableString stringWithString: str1];
[MS insertString: @ "'Huo xing'" atIndex: 5];
[MS insertString: @ "'append in Last ~~~~ '"AtIndex: [MS length];
[MS appendString: @ "append String"];
[MS deleteCharactersInRange: NSMakeRange (10, 15)];

Nsange range = [MS rangeOfString: @ "Hello"];
If (range. location! = NSNotFound ){
NSLog (@ "range location: % I, length: % I", range. location, range. length );
} Else {
Nslog (@ "Location not found !!! ");
}
[MS setstring: @ "Zhongguo"];
[MS replacecharactersinrange: nsmakerange (2, 2) withstring: @ "Chinese"];

46. [MS replaceoccurrencesofstring: @ "Chinese"
Withstring: @ "China"
Options: Nil
Range: nsmakerange (0, [MS length])];
47. nsmutablearray is a subclass of nsarray. The former is variable, and the latter is variable.
48. nsarray:
Nsarray * array = [nsarray arraywithobjects: @ "first", @ "second", @ "third", @ "fourth", @ "th", nil];
For (INT I = 0; I <5; I ++ ){
NSLog (@ "% I: % @", (I + 1), [array objectAtIndex: I]);
}
49. MAC directory:
~ User main directory
. Current Directory
.. Directory at the upper level
50. File Operations:
NSFileManager * fileManager = [NSFileManager defaultManager];
BOOL isRemoveSuc = [fileManager removeFileAtPath: @ "D :\\ demo01.txt" handler: nil];
If (isRemoveSuc ){
NSLog (@ "remove file at path is success ");
} Else {
NSLog (@ "remove file at path is fail ");
}
NSString * fileName = @ "D: \ testfile.txt ";
If ([filemanager fileexistsatpath: Filename] = No ){
Nslog (@ "special file is not exists !!! ");
} Else {
Nslog (@ "special file is exists !!! ");
If ([filemanager copypath: Filename topath: @ "d :\\ testfile02.txt" handler: Nil] = Yes ){
Nslog (@ "copypath: topath: Handler: Is success ");
} Else {
Nslog (@ "copypath: topath: Handler: Is fail ");
}
If ([fileManager contentsEqualAtPath: fileName andPath: @ "D :\\ testfile02.txt"] = YES ){
NSLog (@ "testfile02.txt contents equal testfile01.txt ");
} Else {
NSLog (@ "testfile02.txt contents not equal testfile01.txt ");
}
If ([fileManager contentsEqualAtPath: fileName andPath: @ "D :\\ testfile03.txt"] = YES ){
NSLog (@ "testfile03.txt contents equal testfile01.txt ");
} Else {
NSLog (@ "testfile03.txt contents not equal testfile01.txt ");
}
If ([fileManager movePath: @ "D :\\ testfile02.txt" toPath: @ "D :\\ testfile04.txt" handler: nil] = YES ){
NSLog (@ "movePath: toPath: is success ");
} Else {
NSLog (@ "movePath: toPath: is fail ");
}

NSDictionary * attrs = [fileManager fileAttributesAtPath: fileName traverseLink: NO];
NSLog (@ "file size is % I bytes! ", [[Attrs objectForKey: NSFileSize] intValue]);

/* NSString * contents = @ "kd ";
Contents = [NSString initWithContentsOfFile: fileName encoding: NSUTF8StringEncoding error: nil];
NSLog (@ "D :\\\\ testfile.txt contents is: \ n % @", contents );
*/

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.