iOS Development-Development summary (i)

Source: Internet
Author: User

Summary of various kinky techniques in iOS development (i)

In future articles, the author will be based on some of the key difficulties encountered in the development, simple summary and carding, and will not be too much to explain the relevant concepts.

There may be set to objective-c or swift related to a variety of technologies, although relatively miscellaneous, it is necessary to have a certain development or related basis, or look more miscellaneous, look at the time may also be very messy feeling, but as long as you can carefully read, I believe you will be able to find unexpected harvest, formally, The author named the Summary of kinky technology (don't think too much, haha)!

All right, let's do it now!

OC2.0 new Features:

    • nullability: (optional type for compatible with Swift)
      • -(void) Startwithcompletionblock: (Nullable Void (^) (Nserror * __nullable error)) block;
      • Nullable can be empty __nullable is not empty, when the modified variables need to use __
    • Lightweight generics *: Lightweight fan:
      • -(ObjectType) popobject; @interface
      • Stack<objecttype:id>: nsobject</objecttype:id>
    • __kindof: Return value and strong turn:
    • -(__kindof UITableViewCell *) dequeuereusablecellwithidentifier

Programming Mode:

Object-oriented programming oop (most basic, most primitive programming model)

Functional Programming (responsive) Reactivecocoa (see Related documents)

Value-oriented Programming VOP

Programming pops to the Protocol

Protocol oriented (simple introduction):

1 protocol Errorpopoverrenderer {2  3     func presenterror () 4  5}  6  7 extension errorpopoverrenderer where Self:uiviewcontroller {8  9     func presenterror () {Ten         ///is implemented here by default and provides default parameters for Errorview.     }14 15}

Associative objects: Adding attributes in a category

Header file declares a property

1 @property (nonatomic, copy) NSString *name;

Implementation file

Import <objc/message.h>

Define a static variable

1 static void *key = (void *) @ "Icocos";

Overriding Getter Methods

1 return Objc_getassociatedobject (self, key)

Overriding setter methods

1 objc_setassociatedobject (self, key, name, objc_association_copy_nonatomic)

Wording in Swift:

Directly in the expansion (the category in Swift is expansion)

Circular references

    • Blocks can access local variables, but cannot modify
    • If you modify a local variable, you need to add __block
1  __block int multiplier = 7;2 3      int (^myblock) (int) = ^ (int num) {4 5          multiplier ++;//So it can be 6 7          return nu M * multiplier;8 9      };

__weak __typeof (&*self) weakself =self; Equivalent to __weak uiviewcontroller *weakself =self;

Similar to previous __unsafe_unretain (MRC)

Why not __block? Because the instance variable of self is accessed by reference, Self is retain,block also a strong reference, causing a circular reference, with __week being a weak reference, and when Self is released, weakself is already equal to nil.

Note the point:

Weakself is for the block to not hold self, avoid circular references, and then declare a strongself because once you enter block execution, self is not allowed to be released during this execution. This strongself will be released automatically after block execution, no circular reference issues

1 __weak __typeof (self) weakself = self; 2 3 Self.backgroundtaskidentifier = [application beginbackgroundtaskwithexpirationhandler:^{4     __strong __typeof ( weakself) strongself = weakself; 5 6}];

About block here is a reminder that when using block, you need to pay attention to the return value and parameter delivery, in addition to the circular reference problem!

Three common circular references:

One, Parent-child Mutual holding, commissioned model

1 @interface Ftappcentermainviewcontroller () 2 3 {4 5}6 7  @property (weak,nonatomic) uitableview* mytableview;8 9 @end

The Mytableview in this use the weak modifier.

1 @property (nonatomic, weak) iddelegate; 2 3

"Recommended Method":

Child only the object of the parent is the weak type:

1 @property (nonatomic, weak) iddelegate;

Second, block

Look at the following code:

1 typedef void (^requestnavicallback) (Nsinteger navicode,nsinteger httpcode,nserror * error); 2 3 @interface Ftnavimanager:nsobject4 5 {6 7}8 9 @property (nonatomic, strong)   Requestnavicallback Navicallback;

This is a request navigation class, the class attribute holds the Requestnavicallback, at this time, if the requestnavicallback again holds self, inevitably causes the circular reference.

"Recommended Method":

If there is a circular reference, the compiler prompts for a warning.

If the object does not hold a block object, a circular reference is not generated. If the object holds a block object, it is defined when the block references self:

1 __weak typeof (self) weakself = self;

Third, Nstimer

@interface ftkeepalive:nsobject{    nstimer*              

Implementation file

1 _keepalivetimer = [Nstimer scheduledtimerwithtimeinterval:_expired target:self selector: @selector (  Keeplivestart) Userinfo:nil Repeats:yes];

The class holds the _keepalivetimer,_keepalivetimer and holds the self, causing a circular reference.

"Recommended Method":

Nstimer will hold the object, so: Before deleting the object, the timer's Invalidate method needs to be set.

1-(void) stopkeepalive{2 3     [_keepalivetimer invalidate];4 5     _keepalivetimer = nil;6 7}

@dynamic

iOS Development-Development summary (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.