我发现了类似的问题,但我无法解决我的错误。我的错误是:
没有已知的选择器类方法" initWithUrl:sub:cont:cat:dat'
我已经尝试了
@Synthesize
,self.variableName
代替_variableName
,[[MyClass init] alloc]
,-
更改为+
我该如何解决?
MyClass.h:
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
-(id)init;
-(id)initWithURL:(NSURL *)url_ sub:(NSString *)subject_ cont:(NSString *)content_ cat:(NSString *)category_ dat:(NSString *)date_;
@property NSURL *bannerImageURL;
@property NSString *subject;
@property NSString *content;
@property NSString *category;
@property NSString *date;
@end
MyClass.m:
#import "MyClass.h"
@implementation MyClass
-(id)init {
self = [super init];
if (self) {
_bannerImageURL = [NSURL URLWithString:@"url0"];
_subject = @"sub0";
_content = @"cont0";
_category = @"cat0";
_date = @"dat0";
}
return self;
}
-(id)initWithURL:(NSURL *)url_ sub:(NSString *)subject_ cont:(NSString *)content_ cat:(NSString *)category_ dat:(NSString *)date_ {
self = [super init];
if (self) {
_bannerImageURL = url_;
_subject = subject_;
_content = content_;
_category = category_;
_date = date_;
}
return self;
}
@end
myViewController.m:
#import "myViewController.h"
#import "MyClass.h"
@implementation SimpleTableViewController
MyClass *news1;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *aUrl = [NSURL URLWithString:@"aUrl"];
// Error occurs here.
news1 = [MyClass initWithURL:aUrl sub:@"aSub" cont:@"aCont" cat:@"aCat" dat:@"aDat"];
}
答案 0 :(得分:1)
我认为你的意思是:
news1 = [[MyClass alloc] initWithURL:aUrl sub:@"aSub" cont:@"aCont" cat:@"aCat" dat:@"aDat"];
这是Objective-C中的标准模式:调用[SomeClass alloc]
来创建一个新实例,然后立即调用它上面的一些初始化方法。初始化器是实例方法,必须在类的实例上调用,而alloc
是在类本身上调用的类方法(并返回新分配的该类实例。)