2012-06-14 2 views
0

Big Nerd Ranch의 Objective-C 가이드를 사용하고 있으며 내가 만든 클래스의 모든 항목을 표시하는 데 문제가 있습니다. 더 많은 참고 자료는 제 17 장의 주식에 대한 도전이다. 이 문제에 대한 다른 질문이 있다는 것을 알고 있지만 다른 모든 문제에 대한 수정 된 코드를 확인한 후에도 문제가 계속됩니다. 어떤 이유로 페이스 북 비용 만 표시됩니다. 여기에 내 작품이다 : StockHolding.h수업 중 몇 가지 항목 만 표시되고 있습니까?

#import <Foundation/Foundation.h> 

@interface StockHolding : NSObject 
{ 
    float purchaseSharePrice; 
    float currentSharePrice; 
    int numberOfShares; 
} 

@property float purchaseSharePrice; 
@property float currentSharePrice; 
@property int numberOfShares; 

- (float)costInDollars; 
- (float)valueInDollars; 

@end 

StockHolding.m

#import "StockHolding.h" 

@implementation StockHolding 

@synthesize purchaseSharePrice; 
@synthesize currentSharePrice; 
@synthesize numberOfShares; 


-(float)costInDollars 
{ 

    return numberOfShares*purchaseSharePrice; 
} 


-(float)valueInDollars 
{ 

    return numberOfShares*currentSharePrice; 
} 


@end 

main.m

도와
#import <Foundation/Foundation.h> 
#import "StockHolding.h" 

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

    @autoreleasepool { 

     StockHolding *apple, *google, *facebook = [[StockHolding alloc] init]; 

     [apple setNumberOfShares:43]; 
     [apple setCurrentSharePrice:738.96]; 
     [apple setPurchaseSharePrice:80.02]; 

     [google setNumberOfShares:12]; 
     [google setCurrentSharePrice:561.07]; 
     [google setPurchaseSharePrice:600.01]; 

     [facebook setNumberOfShares:5]; 
     [facebook setCurrentSharePrice:29.33]; 
     [facebook setPurchaseSharePrice:41.21]; 


     NSLog(@"%.2f.", [apple costInDollars]); 
     NSLog(@"%.2f.", [google costInDollars]); 
     NSLog(@"%.2f.", [facebook costInDollars]); 



    } 
    return 0; 
} 

감사합니다!

답변

1
StockHolding *apple, *google, *facebook = [[StockHolding alloc] init]; 

이 줄은 당신이 그들에 항목을 추가 할 때 너무 applegoogle 여전히 nil입니다 마지막 facebook 변수를 할당.

Obj-C가 메시지를 개체에 동적으로 전달하기 때문에 변수에 [google setNumberOfShares:12]으로 항목을 추가하거나 [apple costInDollars]을 호출 할 때 오류가 발생하지 않습니다. 너무 많은

StockHolding *apple = [[StockHolding alloc] init], *google = [[StockHolding alloc] init], *facebook = [[StockHolding alloc] init]; 
+0

감사 :와

보십시오. 큰 도움이됩니다! –