IOS 9有一个有用的特性,可以在属性上添加唯一的约束。IOS 9唯一约束
但是,我希望向上支持IOS 8,除非我将部署目标设置为9,否则无法编译。
是否有一种方法可以创建两个数据模型,并在编译器指令下使用数据模型A和IOS 8以及数据模型B和IOS 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;
}发布于 2015-10-13 11:16:17
这是个很有趣的问题瑞安。我想到的唯一解决办法是:
if #available(iOS 9.0, *) { // add uniqueness constraints here }
更新:--我一直在寻找一种方法,以编程方式将唯一性约束应用于属性,并最终得到了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]]https://stackoverflow.com/questions/33100285
复制相似问题