我创建了一个类,我试图访问公共方法中的一些属性,但我不能,编译器不允许,我真的需要一些帮助。 这是我的代码,我指望你们。 还有其他方法吗?
.h文件
@interface DirController : NSObject
+ (void)startCreate;
+(DirController *)getInstace;
@property (strong, nonatomic) NSString * nomeDiretorio;
@property (strong, nonatomic) NSString * enderecoUrl;
@property (strong, nonatomic) NSString * nomeUsuario;
@property (strong, nonatomic) NSString * senha;
//@property (strong, nonatomic) NSString * nomeCliente;
@property (nonatomic, assign, readonly ) BOOL isCreating;
@property (nonatomic, strong, readwrite) NSOutputStream * networkStream;
.m文件
+ (void)startCreate
{
BOOL success;
NSURL * url;
// assert(self.networkStream == nil); // don't tap create twice in a row!
// First get and check the URL.
url = [[NetworkManager sharedInstance] smartURLForString:self.enderecoUrl];
success = (url != nil);
if (success) {
url = CFBridgingRelease(
CFURLCreateCopyAppendingPathComponent(NULL, (__bridge CFURLRef) url, (__bridge CFStringRef) self.nomeDiretorio, true)
);
success = (url != nil);
}
// If the URL is bogus, let the user know. Otherwise kick off the connection.
if ( ! success) {
} else {
self.networkStream = CFBridgingRelease(
CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url)
);
assert(self.networkStream != nil);
if ([self.nomeUsuario length] != 0) {
success = [self.networkStream setProperty:self.nomeUsuario forKey:(id)kCFStreamPropertyFTPUserName];
assert(success);
success = [self.networkStream setProperty:self.senha forKey:(id)kCFStreamPropertyFTPPassword];
assert(success);
}
self.networkStream.delegate = self;
[self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.networkStream open];
// Tell the UI we're creating.
[self createDidStart];
}
}
答案 0 :(得分:1)
那是因为方法startCreate
不是实例方法,而是类方法。您无法访问类方法中的实例变量。
您可以将starCreate
方法更改为实例方法,方法是在方法开头替换 - 符号的+号(假设您知道自己在做什么)。
答案 1 :(得分:0)
一种类方法就是这样;关于班级的方法。没有实例存储状态。
您需要创建一个类的实例(即MyClass *myThing = [[MyClass alloc] init];
,将您的类方法转换为实例方法,然后它才能正常工作。