Help with understanding C# code and porting to Objective-C

后端 未结 7 1360
心在旅途
心在旅途 2021-01-20 15:35

Ok, I have this prototype that was written by someone else in C# and I\'m trying to put it into Objective-C. Now, I haven\'t had any formal experience with C# yet, so I don\

相关标签:
7条回答
  • 2021-01-20 15:50

    The 4th line is a parameterless constructor and the 5th line is a parameterfull constructor.

    0 讨论(0)
  • 2021-01-20 15:54

    The fourth and fifth lines are constructors in C#. They are the equivalent to [[c_data alloc] init] chains in objective-c. C# allows you to overload constructors based on the parameters they take. This is equivalent to having two different initialization methods in Objective-C:

    @interface CData : NSObject
    {
       double value;
       int label;
       int ID;
    }
    
    @property double value;
    @property int label;
    @property int ID;
    
    -(id) init;
    -(id) initWithValue:(double)value;
    
    @end
    
    0 讨论(0)
  • 2021-01-20 16:00

    The first c_data is a default no-args constructor which initialises the structure's fields to default values (value -> 0.0, label -> 0, ID -> 0) and the second c_data is a constructor which sets the value field of the instance to the passed-in parameter val and the other fields to their default values. What I've described is how those two constructor calls initialise the instance.

    0 讨论(0)
  • 2021-01-20 16:00

    If I may, it is rather like the having both the following methods in an Objective-C class:

    • (id)init;
    • (id)initWithNumber:(NSNumber *)number;

    Constructors and initializers are analogues, they just look a little different.

    0 讨论(0)
  • 2021-01-20 16:02

    4th and 5th are constructors that are used to initialize the instance of c_data when you new up one.

    0 讨论(0)
  • 2021-01-20 16:05

    The fourth is defining a constructor for the class which takes no parameters and has no actions, and the fifth is defining a constructor for the class which takes as a parameter a double value and which sets the class-internal member variable value to the passed value val.

    0 讨论(0)
提交回复
热议问题