代码之家  ›  专栏  ›  技术社区  ›  Iulian Onofrei Denis Oliveira

使Xcode发出关于向数组或字典添加可为null的属性的警告

  •  0
  • Iulian Onofrei Denis Oliveira  · 技术社区  · 6 年前

    是否可以让Xcode对以下代码发出警告:

    // Model.h
    
    @interface Model : NSObject
    
    @property (strong, nonatomic, nullable) NSString *value;
    
    @end
    
    // Model.m
    
    @implementation Model
    
    @end
    
    // ViewController.m
    
    @implementation ViewController
    
    - (void)test {
    
        NSMutableArray<NSString *> *values = [NSMutableArray array];
    
        Model *model = [Model new];
    
        [values addObject:model.value];
    }
    
    @end
    

    ?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Iulian Onofrei Denis Oliveira    6 年前

    不,这在Objective-C中是不可能的。

    不是真正的警告,但如果静态分析器确定会发出警告,它会收到警告 nil ,就像这样:

    NSMutableArray *array = NSMutableArray.array;
    NSString *string = nil;
    [array addObject:string]; // Argument to 'NSMutableArray' method 'addObject:' cannot be nil