gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:49:51 25 4
gpt4 key购买 nike

我正在创建一个应用程序,它加载一个 XML 文件,其中包含来自网络的配置文件列表。有一个按钮,按下时会在 textView 中显示所有配置文件。

现在我想要另一个按钮来从 textView 的列表中删除重复的配置文件,感谢任何有关如何执行此操作的帮助!

我有一个:

NSMutableDictionary *profileDict;

NSMutableArray *配置文件;

NSArray *profileArr;

我已经研究过如何去做,但找不到与我需要的东西相关的东西。我读过一些关于 NSPredicate 的东西,但我是新手,不知道如何让它在我的项目中发挥作用。

用于删除重复项的 IBAction:

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

支持问题的所有类(class)

我把其余的类放在下面,以使一切都清楚。

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

最佳答案

使用下面的代码,但正如@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 "];
}

关于ios - 从 XML 文件 iOS 中消除重复的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com