Objective-C learning notes usage tutorial

Source: Internet
Author: User

Objective-CIs the content of this article, mainly to learnObjective-CAttribute features,NSAID utoreleasepoolUnderstanding, Emacs mobile shortcuts +Common Xcode keysFor more information, see.

1. Objective-C attribute features assign, retain, copy, readonly, readwrite)

Assign: Specifies the setter method to use a simple value assignment, which is the default operation. You can use this attribute for scalar types such as int. You can imagine a float. It is not an object, so it cannot be retain or copy.

Retain: specifies that the retain should be called on a later object. The previous value sends a release message. You can imagine an NSString instance, which is an object and you may want to retain it.

Copy: specifies that the object's copy should be used for deep replication). The previous value sends a release message. Basically like retain, but does not increase the reference count, it is to allocate a new memory to place it.

Readonly: only the getter method is generated, but the getter method is not generated. The getter method has no get prefix ).

Readwrite: the default attribute. The getter and setter methods without additional parameters are generated with only one parameter ).

2. Learn more about NSAID utoreleasepool

A set of stored objects. Objects added to the NSAID pool can be automatically released.

The automatic release actually sends a release message to all objects in the pool when the ngutoreleasepool is cleared or destroyed.

The object is not automatically added to the pool. It is added to the current pool only when the object sends an autorelease message. It can be imagined that the pool is in a stack, and the current pool is at the top of the stack. Every time a new pool is created, it is pushed in. When drain or release is used, it will pop up.

If an object is an Autorelease object, but there is no NSAID utoreleasepool, the system will prompt the memory leakage Just Leaking ). In general, as long as an object is not created using the new, alloc, and copy methods, we assume that the reference count of this object is 1 and added to the NSAID utoreleasepool.

The following code prompts a memory leak:

 
 
  1. NSDate *date = [NSDate date];    
  2. NSLog(@"%@",[date description]);    
  3. return 0;    
  4. /*   
  5.  objc[3466]: Object 0x1001149c0 of class __NSDate autoreleased    
  6.  with no pool in place    
  7.  - just leaking - break on objc_autoreleaseNoPool() to debug   
  8. */    
  9. NSDate *date = [NSDate date];  
  10. NSLog(@"%@",[date description]);  
  11. return 0;  
  12. /*  
  13.  objc[3466]: Object 0x1001149c0 of class __NSDate autoreleased   
  14.  with no pool in place   
  15.  - just leaking - break on objc_autoreleaseNoPool() to debug  
  16. */  

For drain and release problems, you can find instructions here. In a non-GC environment, the two functions the same. Otherwise, you have no reason to use release. If you send drain to the NSAID utoreleasepool, the GC will be reminded to process objects in the pool.

3. Emacs mobile shortcuts + Xcode shortcuts

Although you do not want to learn how to use Emacs, you can record the xcode shortcut keys in the URL address bar of Xcode, TextEdit, and Safari to make them easier to use on Mac.

 
 
  1. Control-F Forward to the right)
  2. Control-B moves to the left Backward)
  3. Control-N down Next) Move a row
  4. Control-P move a row up Previous)
  5. Control-K Delete Kill) code after the cursor
  6. Control-A moves to the beginning of the line with the command + left arrow)
  7. Control-E move to the end of the row with the command + right arrow)
  8. Control-T Transpose, swap) symbols on both sides of the cursor
  9. Control-D Delete) the character on the right of the cursor
  10. Control-L puts the insert point in the center of the window)

Xcode shortcut:

 
 
  1. Command + [move code block left
  2. Command +] Right Shift code block
  3. Tab accept code prompt
  4. Ese display code prompt
  5. Control +. Circular browsing code prompt
  6. Shift + Control +. Reverse browsing code prompt
  7. Control +/move to the next point in the code prompt
  8. Command + Control + S create Snapshot

Summary: AboutObjective-CAttribute features,NSAID utoreleasepoolUnderstanding, Emacs mobile shortcuts +Common Xcode keysThe content of this article has been introduced. I hope this article will help you!

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.