Tried to add a little something to the challenge


So I thought it’d be fun to name my stocks… I got this working, with the predicted output. However, my compiler has a minor issue with it…


[code]#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
//instace variables of StockHolding
NSString *name ;
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;

//accessor methods for instance variables
@property NSString *name;
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars; //purchaseSharePrice * numberOfShares
  • (float)valuesInDollars; //currentSharePrice * numberOfShares



#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares, name;

- (float)costInDollars
//  purchaseSharePrice * numberOfShares
    float p = [self purchaseSharePrice];
    int num = [self numberOfShares];
    return (p*num);

- (float)valuesInDollars
//  currentSharePrice * numberOfShares
    float c = [self currentSharePrice];
    float num = [self numberOfShares];
    return (c*num);


[code]#import <Foundation/Foundation.h>
#import "StockHolding.h"

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

    @autoreleasepool {
        //create instaces of StockHolding
        StockHolding *apple = [[StockHolding alloc] init]; 
        StockHolding *windows = [[StockHolding alloc] init];
        StockHolding *linux = [[StockHolding alloc] init];
        //give stocks values
        [linux setName:@"linux"];
        [linux setPurchaseSharePrice:2.30];
        [linux setCurrentSharePrice:4.50];
        [linux setNumberOfShares:40];
        [windows setName:@"Windows"];
        [windows setPurchaseSharePrice:12.19];
        [windows setCurrentSharePrice:10.56];
        [windows setNumberOfShares:90];
        [apple setName:@"Apple"];
        [apple setPurchaseSharePrice:45.10];
        [apple setCurrentSharePrice:599.51];
        [apple setNumberOfShares:210];
        //create an array of StockHolding instances. "nil" terminates the list
        NSArray *Shares = [NSArray arrayWithObjects: apple, windows, linux, nil];
        //iterate through Shares array
        for (StockHolding *i in Shares){
            NSLog(@"Your stake in %@  is %f \n", [i name], [i valuesInDollars]);
    return 0;

Now… this displays what I wanted, but the compiler doesn’t like it. For @property NSString *name, it says “Default property attribute ‘assign’ not appropriate for non-gc object”… Can someone explain what’s wrong??? It compiles, and displays everything correctly, but I’d love to get rid of this error… Thanks


So Google is my friend. I did a quick search, and I found out that for a string you have to specify copy, retain, or assign. In this case, I edited it as

@property (nonatomical, retain) NSString *name;

This got rid of the errors for me, and now, when it compiles, there no warnings. I have to do more research on this so I can understand it better, but it’s start.


Hey looneylu, thanks for these posts. I too like to add things to the challenges when I’ve completed them.

My addition to this challenge was a little more inane than yours… just getting the listing “nth”-ordered:

        //in main(), after defining the instances, putting values into the instances
        //creating the array etc
        int i=0;
        char* text;
        for (StockHolding *s in myStocks) {
            if (i==1) {text="st";} 
            else if (i==2) {text="nd";} 
            else if (i==3) {text="rd";} 
            else {text="th";};
            NSLog(@"The %d%s stock was bought for $%.2f, and is now valued at $%.2f.",i, text, 
                      [s costInDollars], [s valueInDollars]);

Not bothering to print out ALL the details of the stocks, and even though this list only had three items, my “n-th” text includes the “-th” ending for items that would be more than 3 (i.e. so you could have the 4th, 5th, 6th etc stock).

But I like your addition of stock names… and I was thinking along the lines of stock names, and stock ticker abbreviations. Your posts have solved that for me, thanks!


A minor typo, the word should be nonatomic, rather than nonatomical.