0

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>
Geekoder
  • 1,531
  • 10
  • 21
  • 4
    Don't the constraints contain references to the view objects they, err, constrain? So I suspect the reference to view objects is invalid when the constraint is unarchived. – trojanfoe Jan 30 '14 at 12:07
  • Hmmm May be you are right. So what is the possible solution to this. – Geekoder Jan 30 '14 at 13:05
  • 2
    I think it's difficult to solve as if you save all the information within each constraint, how do you identify the views concerned when you read it back and want to recreate the `NSLayoutConstraint` object? Unless you want to force tags everywhere I don't know. You could see how they are stored in `.xib` files and see if that helps. It would help to know why you want to serialize them in your own storage? – trojanfoe Jan 30 '14 at 13:08
  • OK, so it looks like a unique `id` is given to every element to allow them to be referenced from other elements. – trojanfoe Jan 30 '14 at 13:23

0 Answers0