0

当我在玩 Objective-C 时,我刚刚制作了一个简短的示例演示程序来取乐:

一些代码:

// TestClass.h:    
@interface TestClass : NSObject {
        int someNumber;
        float someFloat;

    }
    @property int someNumber;
    @property float someFloat;

    // Returns String containing some instance values:
    -(NSString *)getNiceString;
    // Returns always the same string:
    -(NSString *)getAnotherString;
    -(id)init;

    @end

--

//TestClass.m:
#import "TestClass.h"

@implementation TestClass

@synthesize someFloat;
@synthesize someNumber;

-(NSString*) getNiceString{

    return [NSString stringWithFormat:
            @"Float number: %f and the number is: %d", self.someFloat, self.someNumber];
}

-(NSString *) getAnotherString{
    return [NSString stringWithString:@"TEST STRING"];
} 

-(id)init{
    self = [super init];
    if(self){
        self.someFloat = 100.34;
        self.someNumber = 324;

        return self;
    }
    return nil;
}
@end

还有一些主要内容:

#import <Foundation/Foundation.h>
#import "TestClass.h"


int main (int argc, const char * argv[])
{

    @autoreleasepool {

        TestClass* instance = [TestClass alloc];
        // Version 2: 
        // TestClass* instance = [[TestClass alloc]init];

        NSLog(@"%@", [instance getNiceString]);
        NSLog(@"%@", [instance getAnotherString]);
    }
    return 0;
}

当我TestClass* instance = [TestClass alloc];在 main 中使用时,输出是:

2013-03-05 09:56:34.767 ObjectiveTest[8367:903] 浮点数:0.000000,数字为:0 2013-03-05 09:56:34.770 ObjectiveTest[8367:903] 测试字符串

当使用第二个版本时(TestClass* instance = [[TestClass alloc]init];):

2013-03-05 10:06:46.743 ObjectiveTest[8421:903] 浮点数:100.339996,数字为:324 2013-03-05 10:06:46.750 ObjectiveTest[8421:903] 测试字符串

The question is if [TestClass alloc] makes any initialization stuff (String is returned properly and values are zeros)... It is worth to mention that if I remove the -(id)init: implementation from TestClass.m the outputs for versions with init and without it are exactly the same... Is there any default initialization?

4

3 回答 3

2

alloc will zero out the memory region. More detail can be found here What happens when alloc or allocWithZone is called?

于 2013-03-05T09:19:34.073 回答
1

alloc doesn't initialize the object correctly, and so must always be used.

The float isn't initialized correctly (0.000 != 100.34) and the string is the result of calling a method which returns a string literal, not an instance variable.

于 2013-03-05T09:16:51.080 回答
1

The question is if [TestClass alloc] makes any initialization stuff (String is returned properly and values are zeros)... It is worth to mention that if I remove the -(id)init: implementation from TestClass.m the outputs for versions with init and without it are exactly the same... Is there any default initialization?

alloc doesn't initialize any member, so if you just call alloc, then someFloat will not be initialized (default value will be 0.0). If you keep away your alloc method from your class implementation the same happens: someFloat will not be initialized and it will have a default value of 0.0 .

But calling just alloc and not init has many disadvantages: all the subclass initializers will not be called, thus you will not be able to use some NSObject's attributes, you shouldn't call just alloc. alloc-init is always used by convention.

于 2013-03-05T09:20:50.967 回答