【发布时间】:2014-01-03 17:49:42
【问题描述】:
我正在创建一个应用程序,该应用程序加载一个带有来自网络的配置文件列表的 XML 文件。按下时有一个按钮可在 textView 中显示所有配置文件。
现在我想要另一个按钮来从 textView 上的该列表中删除重复的配置文件,感谢任何有关如何做到这一点的帮助!
我有一个:
NSMutableDictionary *profileDict;
NSMutableArray *profile;
NSArray *profileArr;
我已经研究过如何做到这一点,但找不到与我需要的东西相关的东西。我读过一些关于 NSPredicate 的东西,但我是新手,不知道如何让它在我的项目中工作。
这是在 textView 中显示配置文件的按钮的代码:
- (IBAction)showProfilesButton:(id)sender
{
XMLParser* parser = [[XMLParser alloc]init]; //This is loading the XMLParser class
[parser loadXML];
NSMutableString *str = [[NSMutableString alloc] init];
for(NSMutableDictionary *obj in [[Globals globalBinding]globalArr])
{
[str appendFormat:@"\n %@", [obj objectForKey:@"firstname"]];
}
[self.profilesLabelTextView setText:str];
}
用于删除重复项的 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
【问题讨论】:
标签: ios objective-c nsmutablearray nsdictionary nspredicate