1

i have defined a new class called StockHolding that has all the methods and instance variables of the class NSObject. And added 3 instance variables:

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

Also, i've added instance methods

- (void)setPurchaseSharePrice:(float)p;
- (void)setCurrentSharePrice:(float)c;
- (void)setNumberOfShares:(int)n;

At main.m file i created an instance of class

StockHolding *stock = [[StockHolding alloc] init];

and want to create and object with variables

StockHolding *a = [stock setPurchaseSharePrice:2.30 setCurrentSharePrice:4.50 setNumberOfShares:40];

but receiving error

No visible @interface for 'StockHolding' declares the selector 'setPurchaseSharePrice:setCurrentSharePrice:setNumberOfShares:'

What i'm doing wrong? Or its just not proper use of inherited instance.

I saw example:

NSCalendar *cal = [NSCalendar currentCalendar];
NSUInteger day = [cal ordinalityOfUnit:NSDayCalendarUnit
                                inUnit:NSMonthCalendarUnit
                               forDate:now];

or this won't work?

oleglestat
  • 13
  • 2
  • maybe you want to use KVO or something similar? – gaussblurinc Jul 17 '13 at 10:45
  • @loldop maybe you're going a little too fast for somebody who clearly is new to objective-C and probably to OOP in general ;) – Cyrille Jul 17 '13 at 11:07
  • @Cyrille sorry, if I too fast, but I didn't see, that you are new to OOP and obj-c:) ok, so, look at this, maybe it can help you ;) [charm about objective-c object handling](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance.html) – gaussblurinc Jul 17 '13 at 11:25
  • Maybe KVO is something too advanced for somebody who hasn't fully grasped the basic concepts of message sending in obj-C, don't you think? – Cyrille Jul 17 '13 at 11:42

3 Answers3

1

You can't just concatenate multiple method calls in a single set of brackets like that. If you want to initialise (create a new instance of) a StockHolding object, you need to declare a method like this:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n;

And implement it similarly to this:

- (void)initWithPurchaseSharePrice:(float)p currentSharePrice:(float)c numberOfShares:(int)n {
    self = [super init];
    if (self) {
        purchaseSharePrice = p;
        currentSharePrice = c;
        numberOfShares = n;
    }
    return self;
}

This is called an initialiser method. It creates a new instance of the class and returns it.

Then, you can use it like this:

StockHolding *a = [[StockHolding alloc] initWithPurchaseSharePrice:2.30 currentSharePrice:4.50 numberOfShares:40];

Note that the actual method names, etc. can be arbitrary and don't have to correlate in any way with the names of your instance variables (unless you override getters and setters for @propertys, but that's another story), since you still have to implement the method yourself, and you can do whatever you want in there.


If you have already created an instance like this:

StockHolding *stock = [[StockHolding alloc] init];

and want to set the values of the variables, you should use @propertys instead of instance variables:

@interface StockHolding : NSObject

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

@end

After that, you can set them directly like this:

StockHolding *stock = [[StockHolding alloc] init];
stock.purchaseSharePrice = whatever;

or

[stock setPurchaseSharePrice:2.30];
[stock setCurrentSharePrice:4.50];

Note that for this to work, you do not have to declare or implement the setters and getters for these variables. In other words, you can safely delete this:

- (void)setPurchaseSharePrice:(float)p;
- (void)setCurrentSharePrice:(float)c;
- (void)setNumberOfShares:(int)n;

because the compiler will add it for you automatically if you use @propertys.

Read this question and its answers for more information on the differences between instance variables and @property.

Community
  • 1
  • 1
Greg
  • 9,068
  • 6
  • 49
  • 91
0

Instead of creating three different messages you can create only one message like this -

- (void)setPurchaseSharePrice:(float)p setCurrentSharePrice:(float)c setNumberOfShares:(int)n;

Then you can call it easily. The example you shown here for NSCalendar is the same thing a single message -

- (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
saadnib
  • 11,145
  • 2
  • 33
  • 54
0

In StockHolding.h

@interface StockHolding : NSObject

@property (nonatomic, assign) float purchaseSharePrice;
@property (nonatomic, assign) float currentSharePrice;
@property (nonatomic, assign) int numberOfShares;

@end

In StockHolding.m

#import "StockHolding.h"

@implementation StockHolding ()

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

@end

Now, every other class where you want to set the values of StockHolding,

  1. Import StockHolding #import "StockHolding.h"

  2. create an object StockHolding *aStockHolding = [[StockHolding alloc] init];

  3. Set Values aStockHolding.numberOfShares = 1; or [aStockHolding setNumberOfShares:1]

By synthesizing the variables, you have by default created the getter and setter methods.


Now, if you want to set all values in one method call..

  1. Declare a method in StockHolding.h

    @interface StockHolding : NSObject
    
    @property (nonatomic, assign) float purchaseSharePrice;
    @property (nonatomic, assign) float currentSharePrice;
    @property (nonatomic, assign) int numberOfShares;
    
    - (void)setPurchaseSharePrice:(float)iPurchase andCurrentSharePrice:(float)iCurrent withTotalNumberOfShares:(int)iTotal;
    
    @end
    
  2. Define method in StockHolding.m

    @implementation StockHolding ()
    
    @synthesize purchaseSharePrice;
    @synthesize currentSharePrice;
    @synthesize numberOfShares;
    
    - (void)setPurchaseSharePrice:(float)iPurchase andCurrentSharePrice:(float)iCurrent withTotalNumberOfShares:(int)iTotal {
      self.purchaseSharePrice = iPurchase;
      self.currentSharePrice = iCurrent;
      self.numberOfShares = iTotal;
    }
    @end
    
  3. Create object in other classes as mentioned above and call

    StockHolding *aStockHolding = [[StockHolding alloc] init];
    [aStockHolding setPurchaseSharePrice:22.3 andCurrentSharePrice:12.22 withTotalNumberOfShares:120]
    
Roshit
  • 1,589
  • 1
  • 15
  • 37