将一个数组中的对象属性与另一个数组中的对象属性进行比较 [英] comparing object attribute in one array to an object attribute in another array
问题描述
我很新,所以当解释时,一些代码会很好。
我有一个字符类和一个characterfound类。代码如下:
@implementation character
@synthesize IDE,name;
- (void)dealloc {
[text release];
[super dealloc];
}
@implementation characterfound
@synthesize IDE;
- (void)dealloc {
[text release];
[super dealloc];
}
我有两个数组用名称和id填充。
如果我想比较只是id的构建一个新的数组或做别的事情。
我如何做到这一点。
例如
* * character [] **
name:joe smith
IDE:ik321
name:james smith
IDE:ik32a
characterfound [] **
IDE:2343k
IDE:ik32a
二,id将被找到,我可以把名称放在另一个数组。
或输出它..
我会尝试解释我的问题,并更具体thnx回复btw。
我有两个类字符类
@interface字符:NSObject {
// attributes
NSInteger type;
NSInteger rep1,rep2,rep3,rep4,rep5;
NSString * name;
NSString * IDE;
}
和characterfound类
@interface characterfound:NSObject {
// attributes
// NSInteger IDE;
NSInteger类型;
NSString * IDE;
}
当我解析一个xml文件时遇到不同的标签并且这样填充我的字符类型
例如
也有一些其他的xml在foundcharacter如下:
所以我的第一个数组将填充字符对象包括它的属性
和第二个数组foundcharacter也将一样。
characterarray =
[character1 name =johnsonid =jfja33,character2 name =smithid =sdfae23]
characterfoundarray
[characterfound ide =jfja33,characterfound2 ide =jap234];
所以我的数组被填充对象及其属性
如果按ID查找字符对象是一个常见的操作,你应该创建一个从id到字符对象的映射,此时从第二个数组进行查找将是微不足道的:
NSMutableDictionary * charactersById = [NSMutableDictionary dictionary];
for(Character * character in characters){
[charactersById setObject:character forKey:[character IDE]];
}
(注意,我已经为你的类大写了示例代码;代码,因为编写忽略标准语言约定的代码在任何语言中都是一个坏主意;它会显着降低代码的可读性。)
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:
@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.
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..
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;
} and the characterfound class
@interface characterfound : NSObject {
// attributes
//NSInteger IDE;
NSInteger type;
NSString *IDE;
}
When I'm parsing an xml file it encounters different tags and such and fills my characterclass accordingly
for instance
also there is some other xml in the foundcharacter like so:
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"]
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.
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:
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.)
这篇关于将一个数组中的对象属性与另一个数组中的对象属性进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!