2

I have a list translated with J2Objc:

var userDtoIds:JavaUtilList = // some list

Now I want to iterate over this list with a for in loop. I tried:

for iten in userDtoIds {
} 

Error: Type 'JavaUtilList' does not conform to protocol 'SequenceType'

With:

 let arr:IOSObjectArray = userDtoIds.toArray()
 for iten in arr {      
 }

I got the Error: Type 'IOSObjectArray' does not conform to protocol 'SequenceType'

The only way how it works is:

for var i:Int32=0; i < userDtoIds.size(); i++ {
}

Can I use a for in loop to iterate over a JavaUtilList?

Edit:

The following code leads to a runtime error:

var list = userDtoIds as! NSArray
for item:String in list as! [String] {
}
Michael
  • 32,527
  • 49
  • 210
  • 370
  • you need to cast userDtoIds to AnyObject or NSArray , follow this [type-anyobject-does-not-conform-to-protocol-sequencetype](http://stackoverflow.com/questions/25563655/type-anyobject-does-not-conform-to-protocol-sequencetype) – Pawan Rai Apr 16 '15 at 08:57
  • @pawan Could you please post an answer. I do not know how to do it. The link does not help me. – Michael Apr 16 '15 at 09:01

1 Answers1

0
extension IOSObjectArray: Sequence {
   func makeIterator() -> Iterator {
       // Create iterator
   }

Here is a wonderful article on conforming custom collection types, even if you are using j2objc.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Corey Pett
  • 628
  • 8
  • 8