将一个数组中的对象属性与另一个数组中的对象属性进行比较

时间:2022-03-04 22:52:51

i seem to be having difficulties in accessing and comparing objects in NSMutableArrays in objective c. I'm very new so when explaining , some code would be nice. I have a character class and a characterfound class. The code looks like this:

我似乎在访问和比较目标c中的NSMutableArrays中的对象时遇到了困难。我很新,所以在解释时,一些代码会很好。我有一个角色类和一个characterfound类。代码如下所示:

@implementation character

@synthesize IDE, name;

- (void) dealloc {
    [text release];
    [super dealloc];
}

@implementation characterfound

@synthesize IDE;

- (void) dealloc {
    [text release];
    [super dealloc];
}

I have two arrays which are being filled with names and id's. If I want to compare just the id's to build a new array or do something else with it. How do I do this.

我有两个数组,其中填充了名称和ID。如果我想只比较id来构建一个新数组或用它做其他事情。我该怎么做呢。

for instance

**character[]**
name :joe smith
IDE: ik321

name :james smith
IDE: ik32a

**characterfound[]**

IDE:2343k
IDE:ik32a 

so when I compare the two ,the id will be found and I can put the name in another array. Or output it..

所以当我比较两者时,会找到id,我可以把名字放在另一个数组中。或者输出它..

I'll try to refrase my question and be more specific thnx for replying btw. I have two classes the character class @interface character : NSObject { // attributes NSInteger type; NSInteger rep1, rep2, rep3, rep4, rep5; NSString *name; NSString *IDE;

我会尝试重新回答我的问题,并且更具体地回答btw。我有两个类的字符类@interface字符:NSObject {//属性NSInteger类型; NSInteger rep1,rep2,rep3,rep4,rep5; NSString *名称; NSString * IDE;

} and the characterfound class

和characterfound类

@interface characterfound : NSObject { // attributes //NSInteger IDE; NSInteger type; NSString *IDE;

@interface characterfound:NSObject {// attributes // NSInteger IDE; NSInteger类型; NSString * IDE;

}

When I'm parsing an xml file it encounters different tags and such and fills my characterclass accordingly

当我解析一个xml文件时,它会遇到不同的标签等,并相应地填充我的字符类

for instance

also there is some other xml in the foundcharacter like so:

在findcharacter中还有一些其他的xml如下:

so my first array will be filled with the character object including it's attributes and the second array foundcharacter will be as well. characterarray = [character1 name="johnson" id="jfja33", character2 name="smith" id="sdfae23"]

所以我的第一个数组将填充包含它的属性的字符对象,第二个数组的findcharacter也是如此。 characterarray = [character1 name =“johnson”id =“jfja33”,character2 name =“smith”id =“sdfae23”]

characterfoundarray [characterfound ide ="jfja33 , characterfound2 ide="jap234" ]; So my arrays are being filled with objects and their attributes and I would like to compare the attributes( if that's possible ) and create an output.

characterfoundarray [characterfound ide =“jfja33,characterfound2 ide =”jap234“];所以我的数组充满了对象及其属性,我想比较属性(如果可能的话)并创建输出。

1 个解决方案

#1


If looking up character objects by ID is a common operation, you should create a mapping from id's to character objects, at which point doing the lookup from your second array will be trivial:

如果通过ID查找字符对象是一种常见操作,则应创建从id到字符对象的映射,此时从第二个数组执行查找将是微不足道的:

NSMutableDictionary* charactersById = [NSMutableDictionary dictionary];
for (Character* character in characters) {
    [charactersById setObject:character forKey:[character IDE]];
}

(Note that I've made your class upper-case for the example code; you should do the same in your code since writing code that ignores standard language conventions is a bad idea in any language; it significantly reduces the readability of your code.)

(请注意,我已经为示例代码创建了大写的类;您应该在代码中执行相同的操作,因为编写忽略标准语言约定的代码在任何语言中都是一个坏主意;它会显着降低代码的可读性。 )

#1


If looking up character objects by ID is a common operation, you should create a mapping from id's to character objects, at which point doing the lookup from your second array will be trivial:

如果通过ID查找字符对象是一种常见操作,则应创建从id到字符对象的映射,此时从第二个数组执行查找将是微不足道的:

NSMutableDictionary* charactersById = [NSMutableDictionary dictionary];
for (Character* character in characters) {
    [charactersById setObject:character forKey:[character IDE]];
}

(Note that I've made your class upper-case for the example code; you should do the same in your code since writing code that ignores standard language conventions is a bad idea in any language; it significantly reduces the readability of your code.)

(请注意,我已经为示例代码创建了大写的类;您应该在代码中执行相同的操作,因为编写忽略标准语言约定的代码在任何语言中都是一个坏主意;它会显着降低代码的可读性。 )