2011-05-30 77 views
0

我必须对Checkin和FriendList进行类。将字符串与NSArray比较

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]; 
    } 
} 

有没有什么更好的办法来做到这一点比较呢?因为它也太慢了。 预先感谢您

回答

1

这不会是帮助:

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

而且

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

没有理由的Alloc他们:只是将其设置在所需的对象索引:

Checkin *tempcheck = [checkinArray objectAtIndex:i]; 

会更好。至于做发现,为什么不循环checkins和每个checkin.profId实例化一个新的NSPredicate来通过好友列表找到profId。尝试[NSPredicate filterWithFormat:@“(profId =%@)”];

然后在阵列上使用filteredArrayUsingPredicate。

+0

这样的事吗? NSString * tempProfID = @“123456”NSPredicate * predicate = [NSPredicate predicateWithFormat:@“profID ==%@”,]; NSArray * results = [directoryContents filteredArrayUsingPredicate:predicate]; – BlackM 2011-05-30 20:19:49

1

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

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

你在做什么是创建一个对象,然后签入阵列中分配指针指向的对象。这会导致内存泄漏。

然后,以后当你做:

[tempcheck release]; 

你actaully调用数组中的对象上发布,而不是一个你刚才alloc'd。这大概会导致阵列中的对象被垃圾收集,然后当您第二次尝试访问它时,会发生崩溃。

取出allocs &版本,只是做这样的事情:

Checkin *tempcheck = [checkinArray objectAtIndex:i];