比较字符串和 NSArray
我必须对 Checkin 和 FriendList 进行分类。
Checkin.h
@interface Checkin : NSObject {
NSString *name;
NSString *profID;
NSString *place;
NSString *photoURL;
NSMutableArray *taggedID;
NSMutableArray *taggedName;
和 Friendlist.h
@interface FriendList : NSObject {
NSString *name;
NSString *profID;
}
我想做的是将每个 checkin.profid(大约 5-6)与friendlist.h(200-5000)进行比较。 我尝试使用 for 循环来执行此操作,但在检查第二个 checkin.profid 时崩溃了。 这是我的方法:
for(int i=0; i<[checkinArray count];i++){
Checkin *tempcheck = [[Checkin alloc] init];
tempcheck = [checkinArray objectAtIndex:i];
for(int j=0;j<[friendsArray count]; j++){
NSLog(@"count %d",j);
FriendList *tempfriend = [[FriendList alloc] init];
tempfriend = [friendsArray objectAtIndex:j];
if([tempcheck.profID isEqualToString:tempfriend.profID]){
NSLog(@"Find prof id same for : %@",tempcheck.name);
break;
}
else
NSLog(@"Not found id same for: %@",tempcheck.name);
[tempfriend release];
}
[tempcheck release];
}
}
有没有更好的方法来进行这种比较?因为它也太慢了。 先感谢您
I have to classes the Checkin and the FriendList.
Checkin.h
@interface Checkin : NSObject {
NSString *name;
NSString *profID;
NSString *place;
NSString *photoURL;
NSMutableArray *taggedID;
NSMutableArray *taggedName;
and the Friendlist.h
@interface FriendList : NSObject {
NSString *name;
NSString *profID;
}
What I am trying to do is to compare each checkin.profid(approximately 5-6) with the friendlist.h(200-5000).
I tried to do it with for loop but when checks the second checkin.profid is crashing.
This is my method:
for(int i=0; i<[checkinArray count];i++){
Checkin *tempcheck = [[Checkin alloc] init];
tempcheck = [checkinArray objectAtIndex:i];
for(int j=0;j<[friendsArray count]; j++){
NSLog(@"count %d",j);
FriendList *tempfriend = [[FriendList alloc] init];
tempfriend = [friendsArray objectAtIndex:j];
if([tempcheck.profID isEqualToString:tempfriend.profID]){
NSLog(@"Find prof id same for : %@",tempcheck.name);
break;
}
else
NSLog(@"Not found id same for: %@",tempcheck.name);
[tempfriend release];
}
[tempcheck release];
}
}
Is there any better way to do this comparison? Because its also too slow.
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不会有帮助:
并且
没有理由分配它们:只需将其设置为所需索引处的对象:
会更好。至于进行查找,为什么不循环检查并为每个 checkin.profId 实例化一个新的 NSPredicate 以通过好友列表查找 profId。尝试 [NSPredicate filterWithFormat:@"(profId = %@)"];
然后在数组上使用filteredArrayUsingPredicate。
this isn't going to be helping:
And
there's no reason to alloc them: just set it to be the object at the desired index:
would be better. As for doing the finds, why not loop through the checkins and for each checkin.profId instantiate a new NSPredicate to find the profId through the friendlist. Try [NSPredicate filterWithFormat:@"(profId = %@)"];
and then use filteredArrayUsingPredicate on your array.
你的内存管理全部坏了。当你做这样的事情时:
你正在做的是创建一个对象,然后将指针分配给签入数组中的对象。这会导致内存泄漏。
然后,稍后当您这样做时:
您实际上是在数组中的对象上调用release,而不是您之前分配的对象。这可能会导致数组中的对象被垃圾收集,然后当您第二次尝试访问它时,您会崩溃。
删除 allocs 和发布并执行如下操作:
Your memory management is all broken. When you do something like this:
What you are doing is creating an object and then assigning the pointer to the object in the checkin array. This causes a memory leak right there.
Then, later on when you do:
you're actaully calling release on the object in the array, not the one you alloc'd earlier. This presumably leads the the object in the array being garbage-collected and then when you try and access it the second time round you get a crash.
Remove the allocs & releases and just do something like this: