Tuesday, December 10, 2013

How to set the initial value of the parent class's attributes?

The following two classes
----------------------------------------------
@ Interface A: NSObject {
NSString * x;
}
@ Property (retain, nonatomic) NSString * x;

@ End
---------
# Import "A.h"
@ Implementation A
@ Synthesize x;
@ End
-----------------------------------------------
# Import
# Import "A.h"

@ Interface B: A {

}
@ End
------------
# Import "B.h"


@ Implementation B

@ End
------------------------------------------------- < br />If the above code, the existing B C D E. .... other types are inherited in Class A,
How do give A property initialized to a value of X @ "Test"

Makes
B * b = [[B alloc] init];
NSLog (b.x);

C * c = [[C alloc] init];
NSLog (b.x);
..........
Can output @ "Test".

For example, in C #, you can write directly
@ Interface A: NSObject {
NSString * x = @ "test";
}
@ Property (retain, nonatomic) NSString * x;

@ End

How do here?
Reply:
Is so write it? uncertain Oh look downstairs bar
Reply:
The init in a
NSString * x = [[NSStringalloc] initWithString: @ "test"];


Reply:
Upstairs you this, I tried not to
Reply:
Add, several common methods I have tried,
And very simple code above, you can try to copy the past,
Make sure no problem to answer.
Reply:
You set a breakpoint inside the init method in the parent class, subclass init to see when there is no jump to the init method of the parent class inside

General parent class init, the output is not a problem
Reply:
Upstairs, I also initially thought this way, the parent class init, but it seems the parent class is the parent class, subclass or call wood.
Reply:
Do not believe you can try, I did not understand why this is so
Reply:
Thank you once Results posted
My own question really is where to write in init
Reply:
Can override the init method to initialize string value in the A category

No comments:

Post a Comment