iOS notes (i)

Source: Internet
Author: User

1.ARC NULL declaration variable

Another advantage of using arc is that all uninitialized variables are "null-valued" by default. This means that a declaration such as the following uses ARC to point to a null value (nil):

NSObject *myobject1,*myobjects2;

Note, however, that unlike other high-level programming languages, ARC does not automatically set the scalar value to zero.

This means that the following line of code declares a variable whose value is not equal to zero:

int A;

int b;

2.OBJECTIVE-C Naming conventions

If you write a method called Newpersonname in the Person object, ARC considers the method to return an assigned object. Calling code and methods compiled with arc (or not applicable to arc) Newpersonname run with normal. But if the class where this method is located does not use ARC compilation and the Calling method uses arc compilation, the program crashes. Conversely, if the Newpersonname method is compiled with arc and the calling code does not, a memory leak occurs.

3. Overriding the default behavior

While it is technically impossible to override LLVM behavior, you can change the method using Clang source callout ns_returns_retained and ns_returns_not_retained. The Newpersonname method can be labeled as follows to tell the ARC compiler that although the method name is preceded by new, it returns an object pointer that has not been retained.

-(NSString *) Newpersonname ns_returns_not_retained;

4. Free Bridge Connection

Unlike the OBJECTIVE-C library, the standard C language and core Fundation class libraries (CF * methods) that we use in objective-c do not follow those naming conventions. This means that the arc compiler is not able to completely release unwanted memory. Before the arc appears, I can force the cf* object into a Ns* object, which is called free bridging (toll-free bridging). That is, we can convert cfstring* from type to nsstring* type. With arc, we can no longer do this, at least not specifying a property transfer modifier.

ARC allows you to use these ownership transfer modifiers:

__bridge

__bridge_retained

__bridge_transfer

1.__bridge

It tells arc not to increase the value of its reference count, nor to change ownership.

2.__bridge_retained

If you want to convert the C pointer type and increase the value of the reference count, we can use the second modifier. We can use this modifier when you want to return a reserved pointer from the Objective-c method, which creates a core Fundation object and then releases it using the Cfrelease method. If the Objective-c method belongs to the Nsreturns_retainded series, it is necessary to return the reserved pointer.

3.__bridge_transfer

If you want to convert the core fundation pointer type to a OBJECTIVE-C pointer and add 1 to the reference count, you can use the last modifier. If the core Fundation method creates an object and wants to manage the memory of the object by arc, we can use this modifier.

5. Ignore Performselector warnings

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-warc-performselector-leaks"

[uiviewcontroller performselector:finishmethod withobject:request];

#pragma clang diagnostic pop

Circular retention due to 6.block

Use __block to avoid cyclic retention (no arc)

__block id safeself = self;

Self.myblock = ^ (nsstring* returnedstring) {

SafeSelf.labelControl.text = returnedstring;

};

Arc changes the semantics of __block, so it should not be used. In Arc, the __block reference is retained rather than copied, which means that the preceding code in the ARC environment will still cause a circular hold. The correct approach is to use __weak or (__unsafe_unretained) references, such as:

  

Use __weak to avoid cyclic retention (with arc)

__weak typeof(self) safeself = self;//ios 5+

__unsafe_unretained typeof(self) safeself = self;//ios 4+

Self.myblock = ^ (nsstring* returnedstring) {

SafeSelf.labelControl.text = returnedstring;

};

iOS notes (i)

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.