IOS Development Review notes (1)-Basic OC knowledge

Source: Internet
Author: User

IOS Development Review notes (1)-Basic OC knowledge
I have been studying IOS for more than three months since I went to work. Because of the weak foundation, I learned from the basic syntax of OC and read the books of grapefruit and grapefruit one after another, now I am looking at the actual programming practice. take this opportunity to make a good summary: 1. naming Conventions: the object type and name are the same to avoid confusion-(void) setURL :( NSString *) URL; // incorrect naming method // change to-(void) setURLString :( NSString *) string;-(void) setURL :( NSURL *) URL; static variables (including scope) start with s, while the complete set variables start with g, in general, avoid using global variables other than constants: static MYThing * sSharedInstance constants starting with k in Cocoa and Core Foundation, but not in Cocoa. It is recommended that (static) constants start with k: static const NSUInteger kMaximumNumberOfRows = 3; NSString * const MYSomethingHappenedNotification = @ "SomethingHappeded"; a post (a, an, the) is usually added to the method parameter name (not very common ), this method can be used to name parameters to avoid confusion with local variables and instance names in the method. instance variables start with an underscore (_) and the class name starts with an uppercase letter, the method name and variable name should start with a lower-case letter, and all class names. The method names must be case-insensitive (that is, the first letter of each word is case-insensitive), rather than underline 2. automatic reference counting ARC is not garbage collection, it is just A compiler optimization, so it cannot handle the issue of circular reference: Drawing 1 garbage collection mechanism if the reference link from an external object to object A is interrupted, objects A and B will be destroyed, but since A and B reference each other in ARC, the reference count is greater than 1, therefore, in IOS development, there are two main types of relationships between strong-referenced management attributes: strong and weak, which are equivalent to re in a non-ARC environment. Long and assign, as long as there is a strong referenced object will always exist, will not be destroyed. When the referenced object is destroyed, the weak reference is automatically set to nil. Therefore, the delegate attribute should always be declared as weak. 3. attribute declares the public attribute in the header file ,. declare private attributes in the m file: copy the code // MyClass. h @ interface class: NSObject @ property (nonatomic, readwrite, weak) id delegate; @ property (noatomic, readonly, strong) NSString * readonlyString; @ end // MyClass. m @ interface MyClass () @ property (noatomic, readwrite, strong) NSString * readonlyString; @ property (noatomic, strong) NSString * privateString; @ end: the compiler automatically creates several variables, including _ delegate, _ readonlyString, and _ privateString. Call these instance variables in nit and dealloc. in the m file, redeclare the readonlyString variable and add a modifier keyword for the setter's private method attribute: 1) atomicity (atomic, nonatomic) it means that the accessor method of the attribute is thread-safe and does not guarantee that the entire object is thread-safe. For example, using NSMutableArray to declare a stuff, using self. stuff and self. stuff = otherstuff (only access is involved), and using objectAtIndex to access the array is NOT thread-safe. However, if attributes do not require access from other threads, it is a great waste to use atomic attributes. Therefore, nonatomic 2 is usually used for read/write attributes (readwrite and readonly) 3) keyword for setting method modifier (weak, strong, copy) Note that for immutable classes such as NSString and NSArray, the copy modifier attribute is used to indicate the object state, the getter method must have no external side effects, so the execution speed is fast and there should be no blocking. Attribute and private instance variable. In @ implementation, you can use private instance variables such as @ implementation {NSString * _ name;}. The default storage type is strong 4. the KVO (Key-Value observation Key-Value Observing) attribute can be automatically observed, when modifying an attribute, you can call the willChangeValueForKey: And didChangeValueForKey: methods. Side effects (the main character is not very familiar with side effects) class or subclass may reference side effects in the setting method. There may be some notifications or events registered with NSUndoManager, which should not be ignored unless necessary. Similarly, classes or child classes may use caching in obtaining methods, but directly accessing instance variables will not use caching. The direct use of instance variables in the multi-threaded code will break through the lock mechanism. Don't worry about the consequences. You shouldn't use the memory: The dealloc method in the memory initialization method: Here _ Value can be used, instead of attribute 5. classification and extended category are considered to be useful. It can reduce inheritance and add new methods to existing classes. Extension Method classification similar to C # is used to modularize a large class into multiple classes that are easy to maintain. The classification Declaration is very simple and similar to the interface. Just write the classification name in the parentheses after the class name: @ interface NSMutableString (Capitalize)-(void) Capitalize @ end

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.