1: Encapsulate some optimization code
Instance A: Same logical code handling for multiple common views
An instance requires that its responsibilities include:
- Load and show avatar pictures with incoming URLs
- Display some ancillary information, such as the flag of the Big V
- Pass the event that the user clicked the avatar to the outer View Controller to jump to the user information page
@interface Fdavatarview:uiview // Suppose Vipinfo is an Entity-(void) Configurewithavatarurl: (Nsurl *) URL Vipinfo: (ID) Info tapped: ( void (^) (void)) block; @end
Then write an extension class to handle the code for the event:
@interface Fdavatarview (Fdavatarviewselfmanager)- (void) Selfmanagedconfigurewithavatarurl: (Nsurl *) URL Vipinfo: (ID) Info UID: (NSString *) uid; @end @implementation Fdavatarview (Fdavatarviewselfmanager)//added a UID parameter for the creation of the latter page- (void) Selfmanagedconfigurewithavatarurl: (Nsurl *) URL Vipinfo: (ID) Info UID: (NSString *UID {[Self configurewithavatarurl:url vipinfo:info tapped:^{ //Assuming the APP structure is Root --TabBar-Navigation-ViewcontrollerUitabbarcontroller *tabbarcontroler = (ID) [Uiapplication.sharedapplication.Delegate. Window.rootviewcontroller; Uinavigationcontroller*navigationcontroller =Tabbarcontroler.selectedviewcontroller; //Create user information View ControllerFduserprofileviewcontroller *profileviewcontroller =[Fduserprofileviewcontroller Viewcontrollerwithuid:uid]; [Navigationcontroller Pushviewcontroller:profileviewcontroller Animated:yes]; }];} @end
Note: If you feel that this coupling is inappropriate, you can also encapsulate a global method to fetch the current top-level Navigation Controller.
b Example: There are several functions on the button like:
Show the number of likes you already have.
Click the button to perform a small animation, like number +1, and send a network request.
If you've already liked it, click to perform the reverse action.
If the network request fails to send, it is back to the pre-click state
@interface Fdlikebutton:uibutton-(void) Configurelikestatus: (BOOL) likeornot count: ( Nsinteger) Count animated: (BOOL) animated; @end
extension classes: @interface Fdlikebutton (Fdlikebuttonselfmanager)- (void) Selfmanagedconfigurewithlikestatus: (BOOL) Likeornot count: (Nsinteger) count; @end @implementation Fdlikebutton ( Fdlikebuttonselfmanager)- (void) Selfmanagedconfigurewithlikestatus: (BOOL) Likeornot count: (Nsinteger) Count {[Self configurelikestatus: Likeornot Count:count Animated:no]; [Self addtarget:self Action: @selector (likebuttontapped:) forcontrolevents:uicontroleventtouchupinside];}- (void) likebuttontapped: (ID) Sender {//+1 or-1 with animation//Network Request ^ (Nserror *error) {//if (error) {//rollback// } // }} @end
2: predicate use
The predicate action in OC is for the array type, he is like the query operation in the database, the data source is an array, the advantage is that we do not need to write a lot of code to manipulate the array, but also play the role of filtering, we can write a simple predicate sentence, we can filter out the array of the data we want. Very convenient. There is no such technology in Java, but an open-source framework has already implemented this functionality.
Let's take a look at a specific example:
Person.h file #import<Foundation/Foundation.h>@interface person:nsobject@property nsstring*Name: @property Nsinteger age;+ (ID) personwithname: (NSString *) name Andage: (Nsinteger) age; @endPerson. m File #import"Person.h"@implementation Person+ (ID) personwithname: (NSString *) name Andage: (nsinteger) age{ person*person =[[Person alloc] init]; Person.name=name; Person.age=Age ; returnPerson ;}-(NSString *) description{NSString*s =[nsstring stringWithFormat:@"name=%@,age=%ld", _name,_age]; returns;} @end
Test method
#import <Foundation/Foundation.h>#import"Person.h"//predicate, specifying the condition of the filter, preserving the Eligible object//verbs are generally used to filter the specified elements in an arrayintMainintargcConst Char*argv[]) {@autoreleasepool {Nsarray*persons =[Nsarray arraywithobjects: [Person Personwithname:@"mac"Andage: -], [person Personwithname:@"1"Andage: -], [person Personwithname:@"2"Andage: +], [person Personwithname:@"3"Andage: -], [person Personwithname:@"4"Andage: -], [person Personwithname:@"5"Andage: -], [person Personwithname:@"6"Andage: -], [person Personwithname:@"7"Andage: +], [person Personwithname:@"8"Andage: -], [person Personwithname:@"9"Andage: +], [person Personwithname:@"0"Andage: the], [person Personwithname:@"Ten"Andage: -], [person Personwithname:@"1"Andage: -]]; //Age less than//defines a predicate object that contains filter conditionsNspredicate *predicate = [Nspredicate predicatewithformat:@"age<%d", -]; //filter The elements in an array using predicate conditions, and then return the results of the query after filteringNsarray *array =[Persons filteredarrayusingpredicate:predicate]; NSLog (@"filterarray=%@", array); //query name=1 and age is greater thanpredicate = [Nspredicate Predicatewithformat:@"name= ' 1 ' && age>40"]; Array=[Persons filteredarrayusingpredicate:predicate]; NSLog (@"filterarray=%@", array); //In (included)predicate = [Nspredicate Predicatewithformat:@"self.name in {' 1 ', ' 2 ', ' 4 '} | | self.age in{30,40}"]; //name begins with apredicate = [Nspredicate Predicatewithformat:@"name Beginswith ' a '"]; //name with the BA endpredicate = [Nspredicate Predicatewithformat:@"name ENDSWITH ' BA '"]; //name contains the character a.predicate = [Nspredicate Predicatewithformat:@"name CONTAINS ' a '"]; //Like matches any number of characters//the condition is satisfied with the S character in name onlypredicate = [Nspredicate Predicatewithformat:@"name like ' *s* '"]; //? Represents a character, the following query condition is: The second character in name is Spredicate = [Nspredicate Predicatewithformat:@"name like '? s '"]; } return 0;}
First we see that we initialize an array of a certain size.
Then we can use the Nspredicate class to do the filtering.
1, querying objects with an age less than 30 in the array//Age less than//defines a predicate object that contains filter conditionsNspredicate *predicate = [Nspredicate predicatewithformat:@"age<%d", -];//filter The elements in an array using predicate conditions, and then return the results of the query after filteringNsarray *array =[Persons filteredarrayusingpredicate:predicate]; NSLog (@"filterarray=%@", array); First create a filter condition://defines a predicate object that contains filter conditionsNspredicate *predicate = [Nspredicate predicatewithformat:@"age<%d", -This is easy to do:@"age<%d", this age is the property name of the person,%D is equivalent to a placeholder, followed by a parameter substitution and then filtered, returning a filtered array object//filter The elements in an array using predicate conditions, and then return the results of the query after filteringNsarray *array =[Persons filteredarrayusingpredicate:predicate];2, Query Name=1 and age greater than 40 of the set//query name=1 and age is greater thanpredicate = [Nspredicate Predicatewithformat:@"name= ' 1 ' && age>40"];array=[Persons filteredarrayusingpredicate:predicate]; NSLog (@"filterarray=%@", array); Of course we can also use&&perform multi-conditional filtering3, including the use of statements//In (included)predicate = [Nspredicate Predicatewithformat:@"self.name in {' 1 ', ' 2 ', ' 4 '} | | self.age in{30,40} "];4, the specified character begins with the end of the specified character, contains the specified character//name predicate = [Nspredicate] predicatewithformat:@"Name Beginswith'a' "];//name with the BA endpredicate = [Nspredicate Predicatewithformat:@"name ENDSWITH ' BA '"];//name contains the character a.predicate = [Nspredicate Predicatewithformat:@"name CONTAINS ' A ' "];5, like to match multiple characters//like match any number of characters//name as long as there is an S character to satisfy the condition predicate = [Nspredicate Predicatewithformat :@"Name like'*s*' "];//? Represents a character, the following query condition is: The second character in name is Spredicate = [Nspredicate Predicatewithformat:@"name like '? s '"];
Application of view-sealed logic code and predicate lookup