Some iOS tips

Source: Internet
Author: User

ARC
What is 1,arc? Automatic referece counting MRC mannual
The technology that came out after iOS5
What is the principle of 2,arc?
When the program is compiled, the system helps us to add retain,release to the appropriate place.
3, cannot be used in Retain,release,autorelease,retaincount
4, use __strong,__weak,__unsafe_unretained these modifiers to describe the variables
4.1, if an object does not have a strong reference pointer to it, it will be released immediately
Usage: The global variable is __strong, and the global __strong pointer is assigned nil when the object of the current class is disposed
Local variables are __strong, and local pointers are assigned nil after they go out of scope
Property description if the object type is with strong, if it is the base data type Unsafe_unretaind or assign
5,autorelease cannot be used, if needed, __autoreleasing (method internally creates objects, returns objects to outside use)
6, connect with __weak
7, resolve circular references in block using __weak
8, the property name cannot start with new, and if you need to start with new, you specify the method name of the Get method
9,arc using the auto-free pool @autoreleasepool {}
Under Arc retain is strong assign is unsafe_unretained
Corefoundation and Foundation Convert the ownership of the Exchange object to each other
OC objects cannot be used in 11,ARC structures
When 12,arc and non-arc are mixed
Some classes under ARC use MRC to compile-fno-objc-arc
Some classes under MRC use ARC to compile-fobjc-arc
13, so that some classes must be used under the arc, if not the ARC environment, the error
/*
#if!__has_feature (OBJC_ARC)
#error must be compiled with arc
#endif
*/

Many common types are available in the Corefoundation framework, but the syntax is based on C
ARC will only help you manage objects in the foundation framework and will not help you manage objects in the core Foundation framework

Cfbridgingretain the ownership of objects in the foundation framework to the COREFOUNDATION framework to handle
NSString *str = @ "str";
Cfstringref cfstr = (cfstringref) cfbridgingretain (str);
Cfrelease (CFSTR);


Created an object in the core Foundation framework
Cfstringref cfstr2 = cfstringcreatewithcstring (Kcfallocatordefault, "str2", kCFStringEncodingUTF8);
NSString *str2 = (__bridge_transfer NSString *) cfstr2;
Depth copy
Shallow copy: Copy of pointer
Deep copy: Copy of Object
Full copy: objects, including objects in objects, are copies
(The encoding and decoding method achieves a full copy)

Summary!!!!!!
Testing for immutable objects of non-container classes
Testing for mutable objects of non-container classes
Objects that are obtained by invoking the Copy method are immutable objects
Call the Mutablecopy method to get all the mutable objects

mutable objects either call copy, or mutablecopy are deep copies

Conclusion: Only immutable object calls copy method is a shallow copy, all the others are deep copy

/*
Implementing a full Copy
Nsmutablearray *array1 = [[Nsmutablearray alloc] initwithobjects:@ "1", @ "2", nil];

Encode the array into data
Nsmutabledata *data = [[Nsmutabledata alloc] init];
Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];
[Archiver Encodeobject:array1];
[Archiver finishencoding];


Decoding data to an array
Nskeyedunarchiver *unarhiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];
Nsmutablearray *array4 = [Unarhiver decodeobject];
[Unarhiver finishdecoding];

NSLog (@ "%p%p", array1,array4);
NSLog (@ "%p%p", [Array1 objectatindex:0],[array4 objectatindex:0]);
*/

Map
Create a location manager (must be written as a global)
_manager = [[Cllocationmanager alloc] init];
_manager.delegate = self;

No request for authorization prior to iOS8
There is no need to configure information in the Info.plist file
Get location information in the process of software use
Nslocationwheninuseusagedescription
Not only in the process of use can also be in the background when the program can also locate
Nslocationalwaysusagedescription


Determine the version number of the system and execute different codes according to the different versions
#define Systemversion [[[Uidevice Currentdevice] systemversion] floatvalue]
if (systemversion >= 8.0)
{
Request permission for
[_manager requestalwaysauthorization];
}

if ([Cllocationmanager locationservicesenabled])
{
NSLog (@ "Can use positioning function");

Some iOS tips

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.