Help, can't call another class's method
Tried many ways based on other questions asked here but could not get a method call to another class to compile.
I'm writing a dedicated class to process arithmetic, for example class Arithmetic. It has no class variables. It subclasses no class (though I tried NSObject with failure). It will have methods of the type:
+ (NSString *)calculateEnergy:(NSString *)weight;
that I want to call from another class, in a static-method way, like:
self.label.text=[arithmetic calculateEnergy:@"8.0"];
where 'Arithmetic *arithmetic' is declared and given a retain property (synthe开发者_StackOverflow社区sised etc.) in the header and implementation of the class wanting to access the calculateEnergy method.
I've tried various suggestions, such as variations on:
[(Arithmetic *) arithmetic calculateEnergy:@"8.0"];
every time getting an "Accessing unknown 'calculateEnergy' getter method"
Thank you so much in advance!
Though I don't have the complete picture, your method definition is for a class-level, static method, as opposed to an instance method. You would want to call it as
self.label.text = [Arithmetic calculateEnergy:@"8.0"];
You also would not need to create an instance of Arithmetic
to call the method you have there.
Is arithmetic
not an instance of Arithmetic
? If instead you try self.label.text=[Arithmetic calculateEnergy:@"8.0"];
you should be OK.
精彩评论