Setting Runtime Attributes in NSLayoutConstraint

I would like to set the property identifierto a few of my limitations from the inline interface. I used the table User Defined Runtime Attributesin IB before with instances NSView, and I had no problems, but I do not do the same with restriction classes anywhere. When I try to access the identifier in the code, it simply returns null, not the row entered in the Value column of the table. I suppose this might be due to the xib loading mechanism, but I hope someone can tell with certainty what the problem is.

IB --> Identity Inspector --> User Defined Runtime Attributes:

  • Key path : identifier
  • Type : string
  • Value : TextViewWidthConstraint

later in IBAction:

NSLog(@"%@", self.widthCon.identifier);

// --> (null)
+4
source share
2 answers

In the NSLayoutConstraint implementation, something seems to be wrong. Creating a category in NSLayoutConstraint and rewriting some methods for debugging purposes revealed the following:

Runtime attributes must be removed by Xcode at compile time for the following reasons:

  • The rewritten KVC methods are inside NSLayoutCategory setValue:forKey:, setValue:forKeyPath:and are setValue:undefinedKey:never called.

  • The rewritten method setIdentifier:is called but never gets the passed value from the runtime attribute section

  • Providing invalid keys does not create errors, as on any other object, such as NSView:

    2014-03-19 08: 25: 52.806 LayoutTestApp [13733: 303] [setValue: forUndefinedKey:]: testKey.

, NSLayoutConstraint Xcode ( init ). , Apple NSLayoutConstraints . : , , atm.

+5

, NSLayoutConstraint identifier, :

NSLog(@"%@", [self.widthCon valueForKey:@"identifier"]);
-1

Source: https://habr.com/ru/post/1531130/


All Articles