# Define collection commonly used in iOS

Source: Internet
Author: User

# Define collection commonly used in iOS
1. When defining constants and defining constants, it is best to start with a lower-case letter k to let people know what they mean. (1) Navigation Bar Height: We all know that the height of the navigation bar is 44 on the iPhone portrait screen, at this time, a constant can be defined to indicate the height. # define kNaivgationBarHeight 44 (2) screen width and height: the screen width and height are the screen size of the iOS device hardware, different from ViewController's view, # define kSCREEN_WIDTH [UIScreen mainScreen]. bounds. size. width # define kSCREEN_HEIGHT [UIScreen mainScreen]. bounds. size. height2: safe release object for memory management # define SAFE_RELEASE (x) [x release]; x = nil note that the end is not; colon, this statement is used in dealloc, for example-(void) dealloc {SA FE_RELEASE (array); [super dealloc];} Why does this statement indicate safe release? When using an Objective-C object, we must ensure that its reference count retainCount is 0, but sometimes we cannot guarantee perfection, at this time, when dealloc is used, the object is set to nil, which releases the memory area of the comrade-in-arms of the object to prevent memory leakage. 3. Determine the version of the iOS system (1) Current System Version # define kCurrentSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue] (2) determine if it is a system version of iOS7 or higher # define IOS_VERSION_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue]> = 7.0 )? (YES) :( NO) (3) Current System Language # define kCurrentLanguage [[NSLocale preferredLanguages] objectAtIndex: 0] 4. defining commonly used colors sometimes multiple controls need to set the same color, while the rgb format of UIColor is indeed a waste of time. Macro-defined constants can save a lot of code, for example, the following defines purple and dark gray, # define kPurpleColor [UIColor colorWithRed: 137.0/255 green: 21.0/255 blue: 89.0/255 alpha: 1.0] # define kDarkGrayColor [UIColor colorWithRed: 100.0/255 green: 100.0/255 blue: 100.0/255 alpha: 1.0] at this time, it is easier to define the background color for the control. 5. Define DLog NS that is more advanced than NSLog. Log facilitates brute-force debugging, that is, to output the value of the variable observed by ourselves. It can be encapsulated more advanced by macro definition, and the following code is used in the pch file of the project, # define DEBUG_MODE 1 # if DEBUG_MODE # define DLog (s ,...) NSLog (@ "<% p % @: (% d) >%@", self, [[NSString stringwithuf8string :__ FILE _] lastPathComponent], _ LINE __, [NSString stringWithFormat :( s), ##__ VA_ARGS _]) # else # define DLog (s ,...) # endif for example, use DLog (@ "12345") in ViewController. The output content on the console is as follows: 19:33:30. 377 DefineSample [3593: 70b] <0x8a68360 ViewController. m :( 54)> 12345 This section contains the memory address of the string @ "12345" <0x8a68360>, the file ViewController. 54 rows of m, the string content is 12345. In fact, let's take a look at the definition of these macros. We can understand more system things, such as _ FILE _, which indicates the FILE to be located and _ LINE _ specifies the row to be located. 6. Determine whether it is an iPhone real Device or a Simulator # if TARGET_ OS _IPHONE // encode NSLog (@ "iPhone Device") for the real Device "); # elif TARGET_IPHONE_SIMULATOR // The NSLog (@ "iPhone Simulator") encoding for the Simulator; # endif sometimes has different performance between the Simulator and the real machine, so you can make a judgment. The above macros TARGET_ OS _IPHONE and TARGET_IPHONE_SIMULATOR are defined by the system and can be used directly. Press and hold the Command button to view more information. 7. Determine if it is ARC // ARC # if _ has_feature (objc_arc) // use arc encoding # else // Manual memory management # endif8, defining the background thread and main thread of GCD // running in the background # define BACK_GCD (block) dispatch_async (dispatch_get_global_queue (queue, 0), block) // The Master thread runs # define MAIN_GCD (block) dispatch_async (dispatch_get_main_queue (), block)

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.