0

I must be missing something simple here. Anyway, I started out by just making a regular function,

NSDecimalNumber* aa(NSMutableString *string)
{code}

which I would then call by pressing a button like so:

- (IBAction)parse:(id)sender {
string1=[NSMutableString stringWithFormat:@"%@", screen.text];
NSDecimalNumber *output=aa(string1);}

(screen.text is from a label) However, partway into it, I realized that the function can't use variables from the rest of my viewcontroller.m class (and vice-versa), so I decided to implement the function as a method instead. Here's what I did. First, I added this to viewcontroller.h,

+ (NSDecimalNumber*) aa:(NSMutableString*) string;
@property (nonatomic, strong) NSDecimalNumber *number; //the number I'm working with

synthesized my property, changed my function declaration to this,

+ (NSDecimalNumber*) aa:(NSMutableString*) string

and attempted to call it like this,

NSDecimalNumber *output=[[NSDecimalNumber alloc] aa:string1];

With that attempt, I got two errors -- "No visible @interface for 'NSDecimalNumber' declares the selector 'aa,'" and "instance variable 'number' accessed in class method."

So I tried again with an instance method. Changed the +'s to -'s and instead called the method with

NSDecimalNumber *output;
[output aa:string1];

That corrected the second error but not the first one. I can't figure out why it isn't recognizing the method in the @interface. Also, those weren't the only things I've tried changing -- I've been playing around with multiple ways to call the method, but nothing seems to work. Any ideas?

2 Answers2

1

This function call:

NSDecimalNumber *output=[[NSDecimalNumber alloc] aa:string1];

..is attempting to call aa an instance of NSDecimalNumber. I don't think that's what you want, isn't your aa method a member of your class? Also, you're not calling a class initializer (although you don't need to, since your method is static so long as its definition starts with +):

// MyClass method definition
+ (NSDecimalNumber*) aa:(NSMutableString*) string

// Called with
NSDecimalNumber *output=[MyClass aa:string1];

--UPDATE--

To address the "instance variable" error, you need to make the method an instance method. Change + in definition to - and call it thusly:

// MyClass method definition
- (NSDecimalNumber*) aa:(NSMutableString*) string

// Call it like this _if calling from within MyClass only_ (hence, "self")
NSDecimalNumber *output = [self aa:string];
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
  • All right, well, changing it to "NSDecimalNumber *output=[ViewController aa:string1];" seems to have fixed the @interface error. But, why is it still saying that the property "number" is an instance variable and then giving me an error for that as well? – classicalpianoguy Jan 17 '13 at 22:30
  • I will update my answer. It means your method can't be static. – Madbreaks Jan 17 '13 at 22:31
  • Sweet! Everything's running smoothly. Thanks for the quick response! – classicalpianoguy Jan 17 '13 at 22:35
0

If you want to add methods to NSDecimalNumber, you need to use a category. Your code adds a method to your view controller subclass.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469