JavaUtilList to NSArray?

987 views
Skip to first unread message

confile

unread,
Apr 14, 2015, 1:16:32 PM4/14/15
to j2objc-...@googlegroups.com
I have a JavaUtilList and want to transform it into an NSArray.

let userList:JavaUtilList = JavaUtilArrayList()

I could create an IOSObjectArray: 

let arr:IOSObjectArray = userList.toArray()

Now I want to have an NSArray of String

var list: [String] = ???


How can I transform a JavaUtilList or an IOSObjectArray into an NSArray?


 

Tom Ball

unread,
Apr 14, 2015, 2:16:26 PM4/14/15
to j2objc-...@googlegroups.com
Create an NSMutableArray, then use a for loop to copy the list or array elements into it. Since j2objc's arrays and JRE collection classes support list comprehension, that can be as simple as:

+ (NSArray *)nsArrayFromList:(id<JavaUtilList>)list {
  NSMutableArray *result = [NSMutableArray array];
  for (id object in list) {
    [result addObject:object];
  }
  return result;
}

This is faster than it looks, since list comprehension uses NSFastEnumeration, which batches the copies.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

confile

unread,
Apr 14, 2015, 2:37:59 PM4/14/15
to j2objc-...@googlegroups.com
Which class contains nsArrayFromList?


I get the error Type 'JavaUtilList' does not conform to protocol 'SequenceType'when I try your for loop.

Tom Ball

unread,
Apr 14, 2015, 2:42:50 PM4/14/15
to j2objc-...@googlegroups.com

Are you trolling? It's an example I just wrote. I don't even know if it compiles, but thought it would at least demonstrate the idea. Apparently not. :(

confile

unread,
Apr 16, 2015, 5:07:53 AM4/16/15
to j2objc-...@googlegroups.com
Is it valid to cast JavaUtilList to NSArray like: 

var arrayList:JavaUtilList = // some java arraylist list 

var list = arrayList as! NSArray

Tom Ball

unread,
Apr 16, 2015, 9:59:23 AM4/16/15
to j2objc-...@googlegroups.com
No, unless you independently create a class that extends NSArray and implements java.util.List. Feel free to contribute such a class if you write it and want to share it with other j2objc developers.

Matthew Pease

unread,
Apr 16, 2015, 12:00:14 PM4/16/15
to j2objc-...@googlegroups.com
+1 I'd like to see such a class. 

I'm just digging into j2objc.  Awesome tool!  But I did spend a fair amount of time figuring out all these new collection types.  Would be great if j2objc could emit objectiveC collection classes.  Though, after just a day using j2objc, I'm learning my way around the types.

Matt
Message has been deleted

confile

unread,
Apr 17, 2015, 7:14:24 AM4/17/15
to j2objc-...@googlegroups.com
You could extend the class like this (in Swift): 

extension JavaUtilArrayList {
  
  func toNSArray<T>() -> [T] {
    var arr:[T] = []
    for var i:Int32=0; i < self.size(); i++ {
      arr.append(self.getWithInt(i) as! T)
    }
    return arr
  }
    
}

kate.w...@ecrebo.com

unread,
Apr 28, 2015, 10:05:24 AM4/28/15
to j2objc-...@googlegroups.com
On Tuesday, 14 April 2015 19:16:26 UTC+1, Tom Ball wrote:
> Create an NSMutableArray, then use a for loop to copy the list or array elements into it. Since j2objc's arrays and JRE collection classes support list comprehension, that can be as simple as:
>
>
> + (NSArray *)nsArrayFromList:(id<JavaUtilList>)list {
>   NSMutableArray *result = [NSMutableArray array];
>   for (id object in list) {
>     [result addObject:object];
>   }
>   return result;
> }
>
>
> This is faster than it looks, since list comprehension uses NSFastEnumeration, which batches the copies.
>

Can confirm it both compiles and works, cribbed it into my Utils lib, saved me a few moments of typing.

Lukasz Kalbarczyk

unread,
Apr 29, 2015, 4:20:54 PM4/29/15
to j2objc-...@googlegroups.com

How can I transform a JavaUtilList or an IOSObjectArray into an NSArray?

I will check it tomorrow, but I'm using something like that:

[NSArray arrayWithObjects: list count: [list size]]]

 (+ (instancetype)arrayWithObjects:(const id [])objects
                           count:(NSUInteger)count)

Lukasz Kalbarczyk

unread,
May 12, 2015, 4:21:22 PM5/12/15
to j2objc-...@googlegroups.com

IOSObjectArray* ar = [objects toArray];
 [NSArray arrayWithObjects: ar->_buffer count: [objects size]]] // or [ar lenght]

vtha...@gmail.com

unread,
Nov 28, 2019, 3:13:02 AM11/28/19
to j2objc-discuss
Reply all
Reply to author
Forward
0 new messages