iOS Development Design Strategy model

Source: Internet
Author: User

Can iOS apps be encrypted? Is there a world of technology that you build? As an iOS app, are you feeling out of it? Let's see what iOS app encryption is:http://www.ijiami.cn/newsInfo?id=541&v=2

In IOS development, using the official framework, the official SDK, can be exposed to a lot of design patterns, may not be noticed, in fact, has used a lot of design patterns

Here's an example:

Policy mode: As to what is the strategy mode, please own Baidu Bar, I also say not clear, but know how to use, the following combination code detailed description

For example, I have a nsmutablearray, inside each element is a nsdictionary, where Nsdictionary has a lot of "key-value" pair, I want to "key 1 corresponding to the value 1" as the standard, the Nsmutablearray to sort.

Nsmutablearray

---nsdictionary1

--"name": "Zhangsan"

--"Age": "30"

---nsdictionary2

--"name": "Lisi"

--"Age": "28"

---nsdictionary3

--"name": "Lisi"

--"Age": "48"

I need to sort on the "Age" field below

So this is how the strategy pattern is presented here: you throw the Nsmutablearray object a sort of method (a strategy), then he takes this method to sort the internal elements, you throw him different methods (that is, different strategies < actual each strategy, not simple is a parameter, Instead of doing the whole process of doing something >), he gives you different results.

Paste the following code

The Nsarray store is nsdictionary, which can be customized using the method of strategy to increase the method of comparison. Then call Nsarray's Sortusingselector method to sort the array, which uses the time order of the time object in Nsdictionay. Here's how:

1. Custom Nsdictionary

XXX.h file

@interface Nsmutabledictionary (Mycompare)

-(Nscomparisonresult) Mycomparemethodwithdict: (nsmutabledictionary*) theotherdict;

@end

XXX.M file

#import "CustomDictionary.h"

@implementation Nsmutabledictionary (Mycompare)

-(Nscomparisonresult) Mycomparemethodwithdict: (nsmutabledictionary*) anotherdict

{

Nsmutabledictionary *firstdict = self;

int iselfage =[[firstdict Objectforkey: @ "age"]intvalue];

int iotherage = [[anotherdict Objectforkey: @ "age"]intvalue];

return [Firstdate compare:seconddate];

nsorderedascending =-1, nsorderedsame, nsordereddescending}

if (iselgage<iotherage) return nsorderedascending;

else if (iselgage==iotherage) return nsorderedsame;

else return nsordereddescending;

}

@end

2. Use Mycomparemethodwithdict to sort the nsarray, assuming Nsarray is an array of Nsdictionary objects read from the plist file.

nsstring* documentsdirectory = [Paths objectatindex:0];

NSString *plistpath = [NSString stringwithformat:@ "%@/xxx.plist", documentsdirectory];

Nsmutabledictionary * CacheData = [[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath];

[Cachearray sortusingselector: @selector (mycomparemethodwithdict:)];//sorted by age Descending

In this way, Cachearray is a sorted array.


iOS Development Design Strategy model

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.