比较字符串和 NSArray

发布于 2024-11-10 16:25:44 字数 1245 浏览 2 评论 0原文

我必须对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2024-11-17 16:25:44

这不会有帮助:

Checkin *tempcheck = [[Checkin alloc] init];
     tempcheck = [checkinArray objectAtIndex:i];

并且

FriendList *tempfriend = [[FriendList alloc] init];
tempfriend  = [friendsArray objectAtIndex:j];

没有理由分配它们:只需将其设置为所需索引处的对象:

Checkin *tempcheck = [checkinArray objectAtIndex:i];

会更好。至于进行查找,为什么不循环检查并为每个 checkin.profId 实例化一个新的 NSPredicate 以通过好友列表查找 profId。尝试 [NSPredicate filterWithFormat:@"(profId = %@)"];

然后在数组上使用filteredArrayUsingPredicate。

this isn't going to be helping:

Checkin *tempcheck = [[Checkin alloc] init];
     tempcheck = [checkinArray objectAtIndex:i];

And

FriendList *tempfriend = [[FriendList alloc] init];
tempfriend  = [friendsArray objectAtIndex:j];

there's no reason to alloc them: just set it to be the object at the desired index:

Checkin *tempcheck = [checkinArray objectAtIndex:i];

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.

北方。的韩爷 2024-11-17 16:25:44

你的内存管理全部坏了。当你做这样的事情时:

 Checkin *tempcheck = [[Checkin alloc] init];
 tempcheck = [checkinArray objectAtIndex:i];

你正在做的是创建一个对象,然后将指针分配给签入数组中的对象。这会导致内存泄漏。

然后,稍后当您这样做时:

[tempcheck release];

您实际上是在数组中的对象上调用release,而不是您之前分配的对象。这可能会导致数组中的对象被垃圾收集,然后当您第二次尝试访问它时,您会崩溃。

删除 allocs 和发布并执行如下操作:

Checkin *tempcheck = [checkinArray objectAtIndex:i];

Your memory management is all broken. When you do something like this:

 Checkin *tempcheck = [[Checkin alloc] init];
 tempcheck = [checkinArray objectAtIndex:i];

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:

[tempcheck release];

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:

Checkin *tempcheck = [checkinArray objectAtIndex:i];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文