(IOS development advanced) Dry Goods Summary

Source: Internet
Author: User

(IOS development advanced) Dry Goods Summary

 


10.1.3 do not send messages to released objects
Some readers want to test whether the retainCount of an object is changed to 0 when it is released. Their test code is as follows: (remember to set it to MRC-fno-objc-arc in Appdelegate. m)
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. NSObject * object = [[NSObject alloc] init]; NSLog (@ Reference Count = % u, [object retainCount]); [object release]; NSLog (@ Reference Count = % u, [object retainCount]); return YES;} // print the result // 10:04:05. 799 MRC_Test [2985: 607] Reference Count = 1 // 10:04:05. 800 MRC_Test [2985: 607] Reference Count = 1

We noticed that the reference count for the last output was not 0. Because the memory of this object has been recycled, and we send a retainCount message to a recycled object, the output result of this object should be uncertain, if the memory occupied by this object is reused, the program may crash abnormally. So why is this uncertain value 1 instead of 0 after this object is recycled. This is because when release is last executed, the system knows that memory will be recycled soon. There is no need to reduce retainCount by 1 again, because no matter whether it is reduced by 1, this object will be recycled. After the object is recycled, all its memory regions, including the retainCount value, also become meaningless. If the value is not changed from 1 to 0, a memory operation can be reduced to Accelerate object recycling.

After using GCD






11.2.4 background running
Another use of block is to allow programs to run in the background for a long time. Previously, when the application was exited by pressing the Home key, the application only had 5 seconds to save or clear resources. However, the application can call the beginBackgroundTaskWithExpirationHandler method of the UIApplication, so that the application can run for up to 10 minutes in the background. This time can be used to clear the local cache and send statistics.
// Appdelegate. h
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;

// Appdelegate. m
LaunchOptions {// Override point for customization after application launch. return YES;}-(void) applicationWillResignActive :( UIApplication *) application {// Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationDidEnterBackground :( UIApplication *) application {[self beginBackgroundUpdateTask]; // Add the code to run for a long time here [self commit];}-(void) beginBackgroundUpdateTask {self. backgroundUpdateTask = [[UIApplication sharedApplication] usage: ^ {[self endBackgroundUpdateTask] ;}];}-(void) usage {[[UIApplication sharedApplication] endBackgroundTask: self. backgroundUpdateTask]; self. backgroundUpdateTask = UIBackgroundTaskInvalid ;}




15.7 use Safari for debugging



 

 

 

 

18.2.2 hide the keyboard

 

Collapse the keyboard in UIViewController. In addition to calling the resignFirstResponder method of the corresponding control, there are three other methods:

1. Reload the touchesBegin method of UIViewController, and then execute [self. view endEdiiting: YES] in it. In this way, click anywhere in UIViewController to collapse the keyboard.

2. Run [[UIApplication sharedApplication] sendAction: @ selector (resignFirstResponder) to: nil from: nil forEvent: nil] directly. This is used when obtaining the UIViewController is difficult.

3. Run [[UIApplication sharedApplication] keyWindow] endEditing: YES] directly.

 

 

18.2.4 set the system control language in the Application

 

 

In iOS applications, you sometimes need to call some system UI controls, such:

1. In UIWebView, press the context menu of the system to pop up.

2. The system camera interface is used in UIImagePickerController.

3. When the UITableViewCell In the compiled state is in the pendingdeletion state, a system deletion button is displayed.

 

The Display language of these UI controls is not the same as the system language of your current mobile phone, but is displayed based on the internal language settings of your application.

 

In the info. plist file, add:

CFBundleLocalizations zh_CN

 

 

18.2.9 ignore compilation warnings

 

 

Use-w to disable all compilation warnings, and use-Wno-unused-variable to only disable compilation warnings for unused variables.

(Same as-fno-objc-arc)

 

 

18.4.3 add images to the simulator album

 

 

... Drag and drop the image directly to the simulator. The image will be opened using Safari and saved.

 

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.