-3

I have two Array holding the instances of class MYItems. FavoriteItems holds the favorite items from the array Items

var Items :[MYItems] = []
var favoriteItems = [MYItems]()

Now i want to check if the Items array contains the favoriteItems in the . How can i acheive this? i tried using the below Find method

if let index = find(favoriteItems, Items[indexPath.row]){

             println("found item")
   }else{
             println("doesnotcontains item")
        }

I tried using the below but that doesnot help too..

 if contains(favoriteItems, Items[indexPath.row]){

            println("found item")

        }else{

         println("doesnot contain item")

        }

But it always goes to the else block? Why is this happening when i contain one of the items in array for sure.

LC 웃
  • 18,888
  • 9
  • 57
  • 72
  • 2
    possible duplicate of [How to check if an element is in an array](http://stackoverflow.com/questions/24102024/how-to-check-if-an-element-is-in-an-array) – milo526 Jun 09 '15 at 07:49
  • Althoug duplicate is about element in an array, the answer still applies and questions are similar – milo526 Jun 09 '15 at 07:50
  • although same...but that doesnot solve for me..so i am asking if there is some another thing to know – LC 웃 Jun 09 '15 at 07:52
  • Could you explain why it does not “help you”. Do you require a index? – milo526 Jun 09 '15 at 07:55
  • if i knew ..why would i post a question here... – LC 웃 Jun 09 '15 at 07:56

4 Answers4

3

I suspect you are searching for the problem in the wrong place. You code on how to search through the array is indeed correct.

var Items :[Int] = [1,2,3,4,5,6,7,8,9,]
var favoriteItems:[Int] = [1,3,4,5,7]

if contains(Items, favoriteItems[3]){
  println("found item")
}else{
  println("doesnotcontains item")
}

Does work in swift 1.2. The fourth item of favoriteItems (5) is indeed inside Items and it also does print found item

For your use with custom classes, you should make your class Equatable to do so you much make the class conform to the Equatable protocol.

func == (lhs:MyItem, rhs:MyItem) -> Bool{
  return lhs.id == rhs.id
}

class MyItem :Equatable {
  let id:Int
  init(id:Int){
    self.id = id
  }
}

var Items :[MyItem] = [MyItem(id: 1), MyItem(id:2), MyItem(id:4), MyItem(id:5)]
var favoriteItems:[MyItem] = [MyItem(id: 1), MyItem(id:2)]

if contains(Items, favoriteItems[1]){
  println("found item")
}else{
  println("doesnotcontains item")
}
halfer
  • 19,824
  • 17
  • 99
  • 186
milo526
  • 5,012
  • 5
  • 41
  • 60
2

Convert the arrays to a Set and then use isSubsetOf:

let itemsSet = Set(Items)
let favoritesSet = Set(favoriteItems)
let result = favoritesSet.isSubsetOf(itemsSet)
Pieter Kuijpers
  • 7,247
  • 5
  • 28
  • 36
0
NSMutableSet *intersection = [NSMutableSet setWithArray:aArray];
[intersection intersectSet:[NSSet setWithArray:bArray]];
NSArray *intrsecArray = [intersection allObjects];

Subset of objects in bArray that are not present in aArray:

NSMutableArray *cArray = [NSMutableArray arrayWithArray:bArray];
[cArray removeObjectsInArray:aArray];
halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73
0

It's very simple, use the following method of NSArray

id commonObject = [array1 firstObjectCommonWithArray:array2];

Ref: https://developer.apple.com/documentation/foundation/nsarray/1408825-firstobjectcommonwitharray?language=objc

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70