2

Just trying to convert some Java code into Objective-C.

Here is a snippet of the code for the constructor of a class called 'bone' written in Java.

public class Bone {

public static double            RADS_TO_DEGS =
                                Quaternion.RADS_TO_DEGS;
public static double            DEGS_TO_RADS =
                                Quaternion.DEGS_TO_RADS;

public static int               LUPA_4CC =
                                FourCCUtil.fourCCInt("LUPA");
public static int               RUPA_4CC =
                                FourCCUtil.fourCCInt("RUPA");
public static int               LLRA_4CC =
                                FourCCUtil.fourCCInt("LLRA");
public static int               RLRA_4CC =
                                FourCCUtil.fourCCInt("RLRA");

protected float                 length;
protected Bone                  parent;
protected Bone[]                childBones;
protected int                   fourCCName;
protected int                   parentFourCCName;
protected float[]               translation;

Here is my attempt at rewriting this code in objective-c:

bone.h

@interface Bone : NSObject{

@property static double         RADS_TO_DEGS;
@property static double         DEGS_TO_RADS;

@property static int                LUPA_4CC;
@property static int                RUPA_4CC;
@property static int                LLRA_4CC;
@property static int                RLRA_4CC;


@protected {
    @property float length;
    @property Bone parent;
    @property Bone[] childBones ;
    @property int fourCCName;
    @property int parentFourCCName;
    @property float[] translation;

bone.m

@implementation VA_Bone

-(id)init{

    self=[super init];
    if(self){

        VA_4ccUtil       *FCC       = [[VA_4ccUtil alloc] init];
        VA_Bone          *Bone      = [[VA_Bone alloc] init];
        VA_TRTransform   *TRTform   = [[VA_TRTransform alloc] init];
        VA_TRUtil        *TRUtil    = [[VA_TRUtil alloc] init];
        VA_Quaternion    *Quat      = [[VA_Quaternion alloc] init]];           

       [Bone setDEGS_TO_RADS: [Quat DEGS_TO_RADS]];
       [Bone setRADS_TO_DEGS: [Quat RADS_TO_DEGS]];

       [Bone setLUPA_4CC: [TRUtil fourCCInt:"LUPA"]];
       [Bone setRUPA_4CC: [TRUtil fourCCInt:"RUPA"]];
       [Bone setLLRA_4CC: [TRUtil fourCCInt:"LLRA"]];
       [Bone setRLRA_4CC: [TRUtil fourCCInt:"RLRA"]];

        [self setParent:NULL];
        [self setFourCCName:0];
        [self setParentFourCCName:0];
        [self setTranslation:NULL];
        [self setLength:0];

    }
    return self;
}

My questions are:

1) Have I declared the static methods correctly?

2) Is it possible to assign class types or is it better to use an array such as NSMutableArray instead of Bone[]?

I am still trying to get my head around how to construct a class in objective-c since I am currently converting some Java code into objective c.

Kind Regards, Sam

2
  • 4
    I dont' see any methods in your definitions at all. Commented Jan 27, 2013 at 21:41
  • 1
    I suggest you to read an Objective-C introduction before attempting to write any code. Clearly you miss the basics. Commented Jan 27, 2013 at 21:42

1 Answer 1

4

Have I declared the static methods correctly?

No. There is no such thing as @property static in Objective C. You need to declare them as class methods:

+(double)RADS_TO_DEGS; // This is a getter

If you need a setter, write one more method:

+(void)setRADS_TO_DEGS:(double)val; // This is a setter

Is it possible to assign class types or is it better to use an array such as NSMutableArray instead of Bone[]?

Plain C arrays are problematic because of the ownership problem. You will be better served with a NSArray property: it would give you the same flexibility, provide your clients with fast enumeration, and deal with resource cleanup in ways idiomatic to Objective C.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok thank you very much for your help, I will have another look at my code and make the changes
Am I able to use this property "@property Bone[] childBones;" or do i need to make "Bone[]" type "NSArray"?
@samb90 No, you need to use @property (readonly) Bone *childBones;, and publish the length of childBones array separately, for example @property (readonly) int childBoneCount;
That won't work. Bone * is already a pointer to a Bone object, but objects are dynamically allocated one-by-one, so it will never be an array of Bone objects. You can either have a C array of Bone * (Bone **) or an NSArray. Cocoa style generally strongly prefers the latter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.