I have implemented auto layout pretty well in each and every project I have done till now. So I have a very good understanding but I got trapped when I wanted to save auto layout objects to a plist file.
Currently I have written below code
NSMutableArray *blueLabelConstraints = [[NSMutableArray alloc] init];
NSArray *blueLabel1 = [GFLayoutConstraint constraintsWithVisualFormat:@"H:[_blueLabel(>=123)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_blueLabel)];
NSArray *blueLabel2 = [GFLayoutConstraint constraintsWithVisualFormat:@"V:|-[_blueLabel(228)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_blueLabel)];
[blueLabelConstraints addObjectsFromArray:blueLabel1];
[blueLabelConstraints addObjectsFromArray:blueLabel2];
[self.coordinatesPlist setObject:[NSKeyedArchiver archivedDataWithRootObject:blueLabelConstraints] forKey:@"BLUE_LABEL_CON"];
The above code I have used to archive and make nsdata which I have saved to plist. The file is written and nsdata is saved. But when I try to unarchive and use nslayout app crashes. I have unarchived in the below manner:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[self getPlistPath]];
NSData *data = [dict objectForKey:@"BLUE_LABEL_CON"];
if (data) {
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"ARRAY _ %@",array);
[self.view addConstraints:array];
}
The array object shows that there are NSLayoutConstraint * objects but when I try to print or use them the app crashes exc_bad_access.
Is the crash because NSLayoutConstraint does not conform NSCoding protocol. What should I do to manage that. I want to take NSLayoutConstraint object in a file and perhaps a plist file. Does anyone have an idea about how to resolve this or how to achieve the same in any ways.
Thanks in advance.
** EDIT ** Well the below are the lines of code I found in the xib when constraints are added. Any idea ??
<constraints>
<constraint firstItem="YiE-Zu-JcX" firstAttribute="top" secondItem="1" secondAttribute="top" constant="21" id="jLB-A7-UBv"/>
</constraints>