1

I have two arrays. One array called albums which contains objects of type Album. The other one is called photosAlbums which contains objects of type PHAlbum. These types are custom classes.

I want to combine the two to form a single array. I have declared an AnyObject array called allAlbums and trying to put the contents of those two arrays in to this one but it's not working.

I tried these methods.

1). Assigned the content of albums array to allAlbums first and then try to append the other array.

self.allAlbums = albums
self.allAlbums.appendContentsOf(photosAlbums)

But I'm getting the error Cannot invoke 'appendContentsOf' with an argument list of type '([PHAlbum])' here.

2). The method from this answer.

self.allAlbums = [albums, photosAlbums].flatMap { $0 }

The type of the combined array becomes [Element].

Is there any other way to do this?

Community
  • 1
  • 1
Isuru
  • 30,617
  • 60
  • 187
  • 303
  • Maybe not exactly a direct answer to your question, but have you tried to make both classes conform to a custom protocol? Then your array can be `[CustomProtocol]` instead of `[AnyObjects]`. – Eendje Feb 15 '16 at 06:26
  • The "duplicate" hopefully answers your question. In your case it would be `self.allAlbums.appendContentsOf(photosAlbums as [AnyObject])`. – Martin R Feb 15 '16 at 06:30
  • You can set the type for PhotoAlbums array as [AnyObject]. This'll resolved your issue – Varun Feb 15 '16 at 06:31

0 Answers0