我已经创建了一个类,并试图访问公共方法内部的一些属性,但我做不到,编译器不允许,我真的需要一些帮助。这是我的密码,我指望你们。有其他方法吗?
.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];
}
}
这是因为方法startCreate
不是实例方法,它是类方法。不能访问类方法内部的实例变量。
您可以将starCreate
方法更改为实例方法,方法是将方法开头的+号替换为-号(假设您知道自己在做什么)。
类方法就是这样;类上的方法。没有可在其中存储状态的实例。
您需要创建一个类的实例(即MyClass *myThing = [[MyClass alloc] init];
,将类方法转换为实例方法,然后它就会正常工作