唯一性约束功能需要ios部署目标9.0或更高版本-核心数据

唯一性约束功能需要ios部署目标9.0或更高版本-核心数据,ios,core-data,xcode7,Ios,Core Data,Xcode7,IOS 9有一个有用的功能,可以在属性上添加唯一的约束 但是,我希望支持IOS 8以上版本,除非将部署目标设置为9,否则无法编译 是否有一种方法可以创建两个数据模型,并在编译器指令中使用数据模型a和IOS 8以及数据模型B和IOS 9及以上 -----更新-------------------- 下面是我用来手动添加唯一约束的代码 - (NSManagedObjectModel *)managedObjectModel { // The managed object model for

IOS 9有一个有用的功能,可以在属性上添加唯一的约束

但是,我希望支持IOS 8以上版本,除非将部署目标设置为9,否则无法编译

是否有一种方法可以创建两个数据模型,并在编译器指令中使用数据模型aIOS 8以及数据模型BIOS 9及以上

-----更新--------------------

下面是我用来手动添加唯一约束的代码

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CL" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        NSArray *entities = [_managedObjectModel entities];
        NSEntityDescription *entity = [entities firstObject];
        NSArray *properties = @[@"serverFileId"];
        [entity setUniquenessConstraints:@[properties]];
    }
    return _managedObjectModel;
}

很有趣的问题,瑞安。我想到的唯一解决办法是:

  • 创建符合iOS8+iOS9的模型(不具有唯一性 限制)
  • 您可以使用
如果#可用(iOS 9.0,*){//在此处添加唯一性约束}

  • 您可以通过编程方式在受保护的范围内添加唯一性约束,如所述
更新: 我一直在寻找一种以编程方式将唯一性约束应用于属性的方法,最终得到了一段
CoreData.NSEntityDescription
的类定义。下面是所需的代码:

/* Returns/sets uniqueness constraints for the entity. A uniqueness constraint is a set of one or more attributes whose value must be unique over the set of instances of that entity.
    Returns/sets an array of arrays, each of which contains one or more NSAttributeDescription or NSString instances (strings must be the names of attributes on the entity) on which the constraint is registered. 
    This value forms part of the entity's version hash. Stores which do not support uniqueness constraints should refuse to initialize when given a model containing such constraints.
    Discussion: uniqueness constraint violations can be computationally expensive to handle. It is highly suggested that there be only one uniqueness constraint per entity hierarchy,
    although subentites may extend a sueprentity's constraint.
*/

@available(iOS 9.0, *)
public var uniquenessConstraints: [[AnyObject]]

你能创建两个不同的模型并在每个iOS上使用它来创建ManagedObjectContext吗?我知道了。对于iOS 8和iOS 9,无法以这种方式打包应用程序:(你想检查IOS 8中的唯一性吗?或者什么都不做?我们需要检查IOS8和IOS9的WAIN。我认为这是正确的方法,我正在寻找如何添加唯一性约束的代码。@ RyhanHeNeNER,请考虑我的答案的更新。可能有你想找到的。哦,你应该把Objc标签放在我的前面,这样我就知道了。不管怎样,很高兴知道它能起作用。