0

我只是好奇,好像无论如何都要分配一个从类方法触发的属性?

前任:

+ (void)asyncResponse:(NSDictionary*)response:(NSError*)error

NSURLConnection在那个调用中得到了响应,但是,我试图在我的另一个类中使用它并将该字典设置为属性,但它给了我错误,因为这是一个类方法。该 asyncResponse 是我用来在运行时定向到任何特定类的“委托调用”。

谢谢。

4

1 回答 1

1

There are lot of options you may use to store the variable. Why do you need a property in class method. Class method is run when there is no role of instance. But, of course you, could pass create an instance of the class in the class method and store the value in the object. Other way would be to create some global variables and assign the values to it such that it can be accessed through out the class.

Creating instance to store the variable into property;

+ (void)asyncResponse:(NSDictionary*)response:(NSError*)error{
  MyClass *me = [[MyClass alloc] init];
  me.someProperty = response;
}

But, I dont think you were looking for this, because it is very simple use of the class and properties.

The other thing you could do is create some static variables inside your implementation and then access those variables through your class. You could even create your own custom getter and setter for it, for more easy uses.

@interface MyClass:NSObject
  +(void)setResponse:(NSDictionary*)response;
  +(NSDictionary*)response;
@end

NSDictionary *globalResponse;
@implementation MyClass

+(void)setResponse:(NSDictionary*)response{
  if(response != globalResponse){
    globalResponse = response;
  }
}

+(NSDictionary*)response{
 return globalResponse;
}

You could set the default value for the response in initialize or load method. This makes a simple class level property.

于 2012-12-03T23:34:56.550 回答