消除XML文件iOS中的重复配置文件

时间:2014-01-06 11:28:38

标签: ios objective-c xml nsmutablearray nspredicate

我正在创建一个应用程序,用于从Web上加载包含配置文件列表的XML文件。按下时,有一个按钮显示textView中的所有配置文件。

现在我想要另一个按钮从textView上的列表中删除重复的配置文件,任何有关如何做到这一点的帮助表示赞赏!

我有:

NSMutableDictionary * profileDict;

NSMutableArray *个人资料;

NSArray * profileArr;

我研究过如何做但却找不到与我需要的东西相关的东西。我已经阅读了一些关于NSPredicate的内容,但我是新手,并且不知道如何使它适用于我的项目。

IBAction删除重复项:

- (IBAction)removeDuplicates:(id)sender
{
    //How should I start here.....?
}

所有支持问题的课程

下面我已经把剩下的课程都搞清楚了。

XMLParser.h

#import <Foundation/Foundation.h>
#import "ViewController.h"

@interface XMLParser : NSObject

{
    bool isStatus;
    XMLParser *currentProfile;
    XMLParser *xmlParser;
    NSXMLParser *parser;
    NSMutableString *currentNodeContent;
    NSMutableArray *profile;
    NSString *firstName;
}

- (void)loadXMLByURL:(NSString *)urlString;
- (void)loadXML;

- (NSString *)firstName;

@property (strong,nonatomic) NSMutableDictionary *profileDict;
@property (strong,nonatomic) NSMutableArray *profile;
@property (strong,nonatomic) NSArray *profileArr;

@end

XMLParser.m

#import "XMLParser.h"
#import "Globals.h"

@implementation XMLParser
-(id)loadXMLByURL:(NSString *)urlString
{
    self.profileDict=[NSMutableDictionary dictionary];
    self.profile=[[NSMutableArray alloc]init];
    self.profileArr=[[NSArray alloc]init];
    NSURL *url = [NSURL URLWithString:@"http://dierenpensionlindehof.nl/profiles1.xml"];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;
}


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile = [XMLParser alloc];
        isStatus = YES;
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"firstname"])
    {

        currentProfile->firstName = currentNodeContent;
    //    NSLog(@"%@",currentProfile->firstName);
        [self.profileDict setObject:currentProfile->firstName forKey:@"firstname"];
    }

    if([elementName isEqualToString:@"profile"])
    {
        [self.profile addObject:self.profileDict];
        self.profileDict=[NSMutableDictionary dictionary];
        [[Globals globalBinding]setGlobalArr:self.profile];
    }
}

-(void)loadXML
{
    [self loadXMLByURL:@"http://dierenpensionlindehof.nl/profiles1.xml"];
}

-(NSString *)firstName
{
    return firstName;
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "XMLParser.h"

@interface ViewController : UIViewController
{

}

@property (weak, nonatomic) IBOutlet UITextView *profilesLabelTextView;

@end

ViewController.m

#import "ViewController.h"
#import "XMLParser.h"
#import "Globals.h"

@interface ViewController ()

@end

@implementation ViewController
 - (IBAction)removeDuplicates:(id)sender
{
}

//Code for the button to display the profiles
- (IBAction)showProfilesButton:(id)sender
{
    XMLParser* parser = [[XMLParser alloc]init];
    [parser loadXML];

    NSMutableString *str = [[NSMutableString alloc] init];

    for(NSMutableDictionary *obj in [[Globals globalBinding]globalArr])
    {
        [str appendFormat:@"\n %@", [obj objectForKey:@"firstname"]];
    }

    [self.profilesLabelTextView setText:str];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Global.h

#import <Foundation/Foundation.h>

@interface Globals : NSObject
@property (strong, nonatomic)NSMutableArray *globalArr;
+(Globals*)globalBinding;

@end

Global.m

#import "Globals.h"
static Globals *global=nil;

@implementation Globals

+(Globals*)globalBinding
{
    if (global == nil) {
        global = [[Globals alloc]init];
    }
    return global;
}

@end

2 个答案:

答案 0 :(得分:2)

为您使用以下代码,但正如@trojanfoe建议的那样,请给自己一些时间来学习一些基本的东西。

- (IBAction)removeDuplicates:(id)sender
{
    //Below line will give you array of names directly from your array
    NSArray *names = [[[Globals globalBinding]globalArr] valueForKeyPath:@"firstname"];
    NSLog(@"Before removing Duplicate->%@",names);

    //Below method will clean remove all duplicate objects and you can create your string again
    NSArray *cleanedArray = [[NSSet setWithArray:names] allObjects];
    NSLog(@"After removing Duplicate->%@",cleanedArray);

    //String without duplicate to show in other textview
    NSString *strwithoutduplicate = [cleanedArray componentsJoinedByString:@"\n "];    
}

答案 1 :(得分:0)

有两种解决方案:

1. Use NSSet. NSSet only gives unique objects from array.

    NSArray *arrayWithUniqueObjects = [[NSSet setWithArray:arrayWithDuplicateObjects] allObjects];

2. a) Take other array b) add objects from first array c) check if it is already added.

    NSMutableArray *firstArray = array with duplicate objects..;
    NSMutableArray *secondArray = [[NSMutableArray alloc] init];
    for (NSString* string in firstArray ) {
         if(![secondArray containsObject:string]) {
            [secondArray addObject:string];
         }
    }

// make string from objects of output array