简体   繁体   中英

Java 2 Objective-C NSArray

I'm using java 2 objective-c convector in order to create iOS app. This convector: http://code.google.com/p/j2objc/

This is my first time of using the objective-c. So I can't solve the problem below:

Originally I have got this line.

static final short[] ar = new short[]{(short)0, (short)0, (short)1,(short)0};

After Java2ObjC I got this. The compilator gives me an error here (short, int, double can't convert to id):

 NSArray * const ar = [NSArray arrayWithObjects:(short)0, (short)0, (short)1, (short)0]

The same problem for double values.

What is the right way to inicialize final static constant array with values?

I've been used used convertor in windows. Now I see that was wrong. Use j2ObjC only in MacOs

The problems is that an NSArray can only hold NSObject or derived classes:

 NSArray * const ar = [NSArray arrayWithObjects:[NSNumber numberWithShort:0], [NSNumber numberWithShort:0], [NSNumber numberWithShort:1], [NSNumber numberWithShort:0], nil];

or with the new shorthand notation:

 NSArray * const ar = @[[NSNumber numberWithShort:0], [NSNumber numberWithShort:0], [NSNumber numberWithShort:1], [NSNumber numberWithShort:0]];

Or use a plain C array:

short const ar[4] = {0,0,1,0};

You aren't using j2objc, you are using https://code.google.com/p/java2objc/ . Completely separate projects, unfortunately with similar names.

Please update your question to refer to the correct project.

Here is the short example to add multiple objects in objective-c

    NSArray *consArr=[[NSArray alloc]initWithObjects:[NSNumber numberWithShort:0], 
   [NSNumber numberWithDouble:0], [NSNumber numberWithInt:1],nil];

You initialize array with the method "arrayWithObjects". You should only add objects, like this

NSNumber * number1 = [NSNumber alloc] initWithShort:0];
NSNumber * number2 = [NSNumber alloc] initWithShort:0];

Then,

NSArray * const ar = [NSArray arrayWithObjects:number1,number2, nil];

should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM