开发者

Is a C# "constructor" the same as an Obj-C "initializer"?

开发者 https://www.devze.com 2023-02-15 03:44 出处:网络
I am very, very new to Obj-C, and will have a ton of questions.(I have the book \"iPhone Programming, The Big Nerd Ranch Guide\", but it doesn\'t address the differences between C# 开发者_Python百科an

I am very, very new to Obj-C, and will have a ton of questions. (I have the book "iPhone Programming, The Big Nerd Ranch Guide", but it doesn't address the differences between C# 开发者_Python百科and Obj-C. Does anyone know of a doc that does address the differences?).

Anyway, my question is above...


In Objective-C, object allocation and initialization are separate operations, but it's common and a good practice to see them called in the context of the same expression:

MyClass *myInstance = [[MyClass alloc] init];

// ...

[myInstance release];

In C#, allocation and initialization happen when you use new:

MyClass myInstance = new MyClass();

The runtime allocates the instance and then calls the constructor.

So yes, the C# constructor is equivalent to the Objective-C initializer, but the usage is different.

Apart from this ... init in Objective-C is just a normal instance method, without any special semantics. You can call it at any point. In C#, constructors are very special static-like methods treated differently by the runtime and with special rules. For example, the C# compiler enforces calls to the base class constructor.


They are similar only as much as you can compare two completely different methods for creating an object. Checkout this information on the Objective-C Runtime.

The following is a very simple (but hopefully not misleading) explanation:

Objective-C

id object = [MyObject alloc]; // allocates the memory and returns a pointer.  Factory-like method from NSObject (unless your class overrides it)
MyObject *myObj = [object init]; // initializes the object by calling `-(id)init`.  You'll want to override this, or a similar init method for all your classes.

Usually written like this:

MyObject *myObj = [[MyObject alloc] init];

From what I know, the C# constructor allocates the memory and calls the appropriate constructor function to initialize the object.

A difference is that in C# you can't call an inherited constructor (see the bottom of the link above) but in Obj-C this will compile, but will give you wrong results.

@interface ClassA : NSObject
- (id) initWithInteger:(int)num;
@end
@interface ClassB : ClassA
- (id) init;
@end
@implementation ClassB
- (id) init
{
    self = [supere initWithInteger:10];
    return self;
}

// main
ClassA *a = [[ClassA alloc] initWithInteger:10]; //valid
ClassB *a = [[ClassB alloc] initWithInteger:10]; // will call init from classA, bypassing and not calling init for classB.

Just be careful with weak/dynamic typed language of Objective-C


They're similar, but not identical. Technically, a constructor fully creates and initializes an instance, while an initializer takes an already constructed instance (usually gotten through alloc) and sets the instance up so that it's ready to be used.

As for the differences between Objective-C and C#: They're two different and mostly unrelated languages. When you're learning a new language, trying to think of it as "Like this language I already know, but with these differences" can actually make it harder to learn, because there are a lot of differences and they're often subtle, so going in with assumptions from another language will confuse you. If you search around Stack Overflow, you'll find a lot of PHP programmers who start to learn a new language and immediately wonder "How do I do variable variables in this language?" It's like looking for a list of the differences between English and Chinese — you're better off not trying to treat one like the other. Keep in mind what you already know, but try not to assume any of it is the same in Objective-C.

0

精彩评论

暂无评论...
验证码 换一张
取 消