标签: class-properties

在`readonly`声明的属性(包括类属性)中,`nonatomic`是否有意义?

编辑:此问题也适用于正常声明的属性(不仅适用于类属性)!

原帖:

让我说我有公共类方法sharedInstance,目前实现为getter方法:

@interface MyClass

+ (instancetype)sharedInstance;

- (void)doSomething;

@end

@implementation MyClass

+ (instancetype)sharedInstance {
    static MyClass *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[MyClass alloc] init];
    });
    return shared;
}

@end
Run Code Online (Sandbox Code Playgroud)

在Swift 3.0中访问此方法将如下所示: MyClass.shared().doSomething()

因此,为了使它更加开放,我们应该将类方法更改为类属性(Xcode 8中的新属性.但实际上我无法在Apple Docu中找到它,仅在WWDC 2016视频中找到它)

@interface MyClass

@property (class, nonatomic, readonly) MyClass *sharedInstance;

- (void)doSomething;

@end

// implementation stays the same
Run Code Online (Sandbox Code Playgroud)

现在在Swift代码中: MyClass.shared.doSomething()

那么nonatomic/atomic属性修饰符(不知道确切的术语)甚至对我在objc中实现的getter方法有意义吗?

objective-c ios declared-property swift class-properties

7
推荐指数
1
解决办法
239
查看次数

可归属性

我有一个库(django-piston),它期望类的一些参数作为类属性.我想在一个方法中动态定义这个值.所以我想做一些事情:

class MyHandler(BaseHandler):
    @property
    def fields(self):
        fields = self.model._meta.fields + self.model._meta.virtual_fields
        # Do something more with fields
        return fields
Run Code Online (Sandbox Code Playgroud)

但它失败了:

'property' object is not iterable
Run Code Online (Sandbox Code Playgroud)

所以我想做一些事情:

class iterable_property(property):
    def __iter__(self):
        # What here?
Run Code Online (Sandbox Code Playgroud)

但我被困在这里.我怎么能得到一个也可以迭代的属性?

python class-properties

6
推荐指数
1
解决办法
4324
查看次数

如何创建抽象类属性(可能是只读的)

我花了很多时间研究这个,但没有一个答案看起来像我想要的那样.

我有一个带有类属性的抽象类,我希望每个子类都被强制实现

class AbstractFoo():
    forceThis = 0
Run Code Online (Sandbox Code Playgroud)

所以,当我这样做

class RealFoo(AbstractFoo):
    pass
Run Code Online (Sandbox Code Playgroud)

它会抛出一个错误,告诉我在实现之前它无法创建类forceThis.

我怎样才能做到这一点?

(我不希望该属性是只读的,但如果这是唯一的解决方案,我会接受它.)

对于一个类方法,我发现我可以做到

from abc import ABCMeta, abstractmethod

class AbstractFoo(metaclass=ABCMeta):
    @classmethod
    @abstractmethod
    def forceThis():
        """This must be implemented"""
Run Code Online (Sandbox Code Playgroud)

以便

class RealFoo(AbstractFoo):
    pass
Run Code Online (Sandbox Code Playgroud)

至少抛出错误 TypeError: Can't instantiate abstract class EZ with abstract methods forceThis

(虽然它不强制forceThis成为一种类方法.)

如何为类属性弹出类似的错误?

python abstract-class exception class-properties

5
推荐指数
1
解决办法
1257
查看次数

在 React 中对类属性使用箭头函数。不清楚

我发现箭头函数功能被用作 React 组件中的 Class 属性。在网上查看,我发现它使代码更具可读性,并且由于箭头函数功能,我们不必在构造函数内绑定handlEvents函数。

即使在对类属性使用箭头函数时,我仍然必须使用绑定方法,如下面的代码所示。当我删除构造函数中的绑定时,它在控制台中显示错误Warning: A component is changing an uncontrolled input of type text to be controlled.,并且表单错误也不会显示

class Contact extends Component {
    constructor(props) {
        super(props);

        this.handleBlur = this.handleBlur(this);
    }

    handleBlur = evt => field => {
        this.setState({
        touched: { ...this.state.touched, [field]: true }
    });

   render() {
       return(
          <Form onSubmit={this.handleSubmit}>
            <FormGroup row>
              <Label htmlFor="firstname" md={2}>
                First Name
              </Label>
              <Col md={10}>
                <Input
                  type="text"
                  id="firstname"
                  name="firstname"
                  placeholder="First Name"
                  valid={errors.firstname === ""}
                  invalid={errors.firstname !== ""}
                  value={this.state.firstname}
                  onBlur={event => {
                    this.handleBlur("firstname"); …
Run Code Online (Sandbox Code Playgroud)

this reactjs class-properties arrow-functions

5
推荐指数
1
解决办法
2006
查看次数

如何在每个类方法调用之前和之后执行函数?

我想在 javascript 类中的函数上插入预执行和后执行挂钩。

假设我有一堂这样的课。

class Foo {
  method1(p1, p2) {
    this.p1 = p1;
    this.p2 = p2;
  }

  method2(p3) {
    this.p3 = p3;
  }
}
Run Code Online (Sandbox Code Playgroud)

我想为这些预先存在的类方法定义一个 before 和 after 钩子。像这样的东西。

class Foo {
  before(funName, ...params){
    // Should print ('method1', [p1, p2]) when method 1 is called
    // and ('method2', [p3]) when method 2 is called
    console.log(funName, params)
  }
  after(funName, result){
    // Should print the function name followed by its result
    console.log(funName, result)
  }
  method1(p1, p2) {
    this.p1 = p1;
    this.p2 = …
Run Code Online (Sandbox Code Playgroud)

javascript hook class-properties

3
推荐指数
1
解决办法
8524
查看次数