我是Objective-c的新手,我遇到了一些问题。我的代码似乎检查出来,因为xcode说我没有问题,但在构建它时我说我在Molecule中的所有变量都是零。我似乎无法弄明白。让我知道如果发布我的元素类是必要的,谢谢。我愿意接受任何纠正批评并愿意学习。
这是main.m
@autoreleasepool {
// insert code here...
Element *myElement1 = [[Element alloc] initWithName:@"Hydrogen" andSymbol:@"H" andAtomicNumber:1 andAtomicWeight:1.00794];
NSLog(@"The element %@ has atomic number %li.", [myElement1 name], (long)[myElement1 atomicNumber]);
NSLog(@"The element is %@.", [myElement1 description]);
Element *myElement2 = [[Element alloc] initWithName:@"Hydrogen" andSymbol:@"H" andAtomicNumber:1 andAtomicWeight:1.00794];
NSLog(@"The element is %@.", [myElement2 description]);
NSLog(@"The element %@ has atomic number %li.", [myElement2 name], (long)[myElement2 atomicNumber]);
Element *myElement3 = [[Element alloc] initWithName:@"Oxygen" andSymbol:@"O" andAtomicNumber:8 andAtomicWeight:15.9994];
NSLog(@"The element is %@.", [myElement3 description]);
NSLog(@"The element %@ has atomic number %li.", [myElement3 name], (long)[myElement3 atomicNumber]);
Molecule *myMolecule = [[Molecule alloc] initWithName:@"Water" andSymbol:@"H2O" andElement1:myElement1 andElement2:myElement2 andElement3:myElement3 andWeight:33.00674];
NSLog(@"The molecule is %@.", [myMolecule description]);
NSLog(@"The molecule %@ has symbol %@ and atomic number %li.", [myMolecule name], [myMolecule symbol],(long)[myMolecule weight]);
}
return 0;
}
这里是Molecule.h
@interface Molecule : NSObject{
NSString *name;
NSString *symbol;
Element *element1;
Element *element2;
Element *element3;
float weight;
}
-(id)init;
-(id)initWithName:(NSString *)newName;
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol;
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *)newElement1;
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *)newElement1 andElement2:(Element *)newElement2;
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *)newElement1 andElement2:(Element *)newElement2 andElement3:(Element *)newElement3;
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *)newElement1 andElement2:(Element *)newElement2 andElement3:(Element *)newElement3 andWeight: (float)newWeight;
-(void)setName:(NSString *)newName;
-(NSString *)name;
-(void)setSymbol:(NSString *)newSymbol;
-(NSString *)symbol;
-(void) setElement1:(Element *)newElement;
-(Element *)element1;
-(void) setElement2:(Element *)newElement;
-(Element *)element2;
-(void) setElement3:(Element *)newElement;
-(Element *)element3;
-(void)setWeight:(float) newWeight;
-(float)weight;
-(NSString *)description;
@end
这是Molecule.m
@implementation Molecule
-(id)init
{
return [self initWithName:@"No Name"];
}
-(id)initWithName:(NSString *)newName
{
return [self initWithName:newName andSymbol:@"?"];
}
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol
{
return [self initWithName:newName andSymbol:newSymbol andElement1:element1];
}
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *) newElement1
{
return [self initWithName:newName andSymbol:newSymbol andElement1:element1 andElement2:element2];
}
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *) newElement1 andElement2:(Element *) newElement2
{
return [self initWithName:newName andSymbol:newSymbol andElement1:element1 andElement2:element2 andElement3:element3];
}
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *) newElement1 andElement2:(Element *) newElement2 andElement3:(Element *)newElement3
{
return [self initWithName:newName andSymbol:newSymbol andElement1:element1 andElement2:element2 andElement3:element3 andWeight:0.0];
}
// Designated Initializer
-(id)initWithName:(NSString *)newName andSymbol:(NSString *)newSymbol andElement1:(Element *)newElement1 andElement2:(Element *)newElement2 andElement3:(Element *)newElement3 andWeight:(float)newWeight
{
self = [super init];
if (self) {
[self setName:newName];
[self setSymbol:newSymbol];
[self setElement1:element1];
[self setElement2:element2];
[self setElement3:element3];
[self setWeight:newWeight];
}
return self;
}
-(void)setName:(NSString *)newName
{
name = newName;
}
-(NSString *)name
{
return name;
}
-(void)setSymbol:(NSString *)newSymbol
{
symbol = newSymbol;
}
-(NSString *)symbol
{
return symbol;
}
-(void)setElement1:(Element *)newElement1
{
element1 = newElement1;
}
-(Element *)element1
{
return element1;
}
-(void)setElement2:(Element *)newElement2
{
element2 = newElement2;
}
-(Element *)element2
{
return element2;
}
-(void)setElement3:(Element *)newElement3
{
element3 = newElement3;
}
-(Element *)element3
{
return element3;
}
-(void)setWeight:(float)newWeight
{
weight = [element1 atomicWeight]+ [element2 atomicWeight]+ [element3 atomicWeight];
}
-(float)weight
{
return weight;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"\nName : %@\nSymbol: %@\nElement1 :%@\nElement2 :%@\nElement3 : %@\nWeight : %f\n", name, symbol, element1, element2, element3, weight];
}
@end
我的输出是:
2014-01-28 12:33:53.166 MyFirstApp[14239:303] The element Hydrogen has atomic number 1.
2014-01-28 12:33:53.168 MyFirstApp[14239:303] The element is
Name : Hydrogen
Symbol: H
Atomic Number : 1
Atomic Weight : 1.007940
.
2014-01-28 12:33:53.168 MyFirstApp[14239:303] The element is
Name : Hydrogen
Symbol: H
Atomic Number : 1
Atomic Weight : 1.007940
.
2014-01-28 12:33:53.168 MyFirstApp[14239:303] The element Hydrogen has atomic number 1.
2014-01-28 12:33:53.169 MyFirstApp[14239:303] The element is
Name : Oxygen
Symbol: O
Atomic Number : 8
Atomic Weight : 15.999400
.
2014-01-28 12:33:53.169 MyFirstApp[14239:303] The element Oxygen has atomic number 8.
(lldb)
这就是变量视图中的内容
myElement2 Element * 0x10010d610 0x000000010010d610
myElement3 Element * 0x100300340 0x0000000100300340
myElement1 Element * 0x100102e90 0x0000000100102e90
myMolecule Molecule * nil 0x0000000000000000
NSObject NSObject
name NSString * nil
symbol NSString * nil
element1 Element * nil
element2 Element * nil
element3 Element * nil
weight float