在 xcode 中,如何调用返回值的类方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11858628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:10:30  来源:igfitidea点击:

In xcode, how to call a class method which returns a value?

objective-ciosxcode

提问by Rian Smith

I'm new to Objective-C and for the life of me cannot get past the error, "no class method for selector"

我是 Objective-C 的新手,我一生都无法克服错误,“没有选择器的类方法”

Here is my .h code:

这是我的 .h 代码:

#import <UIKit/UIKit.h>

@interface PhotoCapViewController : UIViewController < UIImagePickerControllerDelegate, UINavigationControllerDelegate > {
    UIImageView * imageView;
    UIButton * choosePhotoBtn;
    UIButton * takePhotoBtn;
}
@property (nonatomic, retain) IBOutlet UIImageView * imageView;
@property (nonatomic, retain) IBOutlet UIButton * choosePhotoBtn;
@property (nonatomic, retain) IBOutlet UIButton * takePhotoBtn;
- (IBAction)getPhoto:(id)sender;

+ (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img;

@end

Here is the function I have defined in .m

这是我在 .m 中定义的函数

+ (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {

    ...

    return theImage;
}

Here is how I'm calling the function

这是我调用函数的方式

UIImage* image1 = [PhotoCapViewController burnTextIntoImagetext:text1 img:imageView.image];

Any help would would be appreciated. Thanks.

任何帮助将不胜感激。谢谢。

回答by jrturton

The method you are calling doesn't match the definition.

您调用的方法与定义不匹配。

The definition is this:

定义是这样的:

+ (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img;

so the method name is this:

所以方法名称是这样的:

burnTextIntoImage::

But you call it like this:

但是你这样称呼它:

UIImage* image1 = [PhotoCapViewController burnTextIntoImagetext:text1 img:imageView.image];

so you're trying to call a method named this:

所以你试图调用一个名为 this 的方法:

burnTextIntoImagetext::

You could call it correctly like this:

你可以像这样正确地调用它:

UIImage* image1 = [PhotoCapViewController burnTextIntoImage:text1 :imageView.image]; 

Though really, your method should be called burnText:(NSString*)text intoImage:(UIImage*)image, so it makes more of a "sentence", like this:

虽然实际上,您的方法应该被调用burnText:(NSString*)text intoImage:(UIImage*)image,因此它更像是一个“句子”,如下所示:

+ (UIImage *)burnText:(NSString *)text intoImage:(UIImage *)image;

...

UIImage *image1 = [PhotoCapViewController burnText:text1 intoImage:imageView.image];

回答by Desdenova

Your method declaration is incomplete.

您的方法声明不完整。

Change

改变

+ (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img;

to

+ (UIImage *)burnTextIntoImage:(NSString *)text img:(UIImage *)img;

回答by Neo

Your calling code is wrong, try to use this code

您的调用代码有误,请尝试使用此代码

UIImage* image = [PhotoCapViewController burnTextIntoImage:text1 img:imageView.image];