Why doesn't UIView (or subclasses) accept the NSCopying protocol?

Can Cocoahead explain why UIView and its subclass do not accept NSCopying?

I can philosophically understand why UITouch is not compatible with the copy, as it is a very temporary object. By UIView, and these are subclasses, especially UIButton, it looks like they should be copied.

Of course, Apple has good reason to do what they do. Do you know their reason?

+3
source share
3 answers

, , , " ?" "?" . . NSCoding (.. Interface Builder), .

+4

. , , , , Apple ... ; , .

, , UIView , NSCopying .

+4

Because it is NSCopyingnot so good in deep (recursive) copies of object plots. For example, [NSArray copy]copies a list of objects, not the objects themselves. Object schedules are better served NSCoding. Which in a happy coincidence is supported UIView.

If you want to copy a custom view with properties, you will need to maintain NSCoding. For example.

@interface SKCustomCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel* nameLabel;
@property (strong, nonatomic) IBOutlet UIView* topView;

@end


static NSString* propertiesKey = @"SKCustomCellProperties";

@implementation SKCustomCell

@synthesize nameLabel;
@synthesize topView;

- (id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder: aDecoder];
    [self setValuesForKeysWithDictionary: [aDecoder decodeObjectForKey: propertiesKey]];
    return self;
}

- (void) encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder: aCoder];

    [aCoder encodeObject: [self dictionaryWithValuesForKeys: [[NSArray alloc] initWithObjects: @"nameLabel", @"topView", nil] forKey: propertiesKey];
}


@end
+3
source

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


All Articles