아래 블록을 실행할 때. 내 friends_dict
friend
변경으로 NSMutableDictionary 값이 변경됩니다. 내가 잘못하지 않았다면 이것은 사전에있는 값의 참조가 friend
에 전달 되었기 때문입니다. 참조 대신 값을 전달하고 사전이 변경되지 않도록하려면 어떻게해야합니까?iOS가 참조 대신 값을 전달합니다.
NSMutableArray *friendsArray = [[NSMutableArray alloc] initWithArray:[friends_dict valueForKey:selectedFriendId]];
for (NSObject *object in [response objectForKey:@"friend_nearby"]){
NSString *id = [NSString stringWithFormat:@"%@",[object valueForKey:@"id"]];
NSString *spotValue = [NSString stringWithFormat:@"%@",[object valueForKey:@"spot"]];
for(int i = 0; i < [friendsArray count]; i++){
FriendDataModel *friend = [[FriendDataModel alloc] init];
friend = [friendsArray objectAtIndex:i];
if ([friend.friendId isEqualToString:id] && ![friend.spot isEqualToString:spotValue]) {
friend.spot = spotValue; //This is where the dictionary value changes
[friendsArray replaceObjectAtIndex:i withObject:friend];
spotChanged = YES;
}
}
}
열거하는 동안 배열을 변경하지 마십시오. – zaph
개체의 복사본을 전달하십시오. –