Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
TableSearch/APLProduct.m
/* |
Copyright (C) 2013-2015 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
Abstract: |
Simple class to represent a product, with a product type and name. Provides methods to retrieve the names of the available types of product, and the localized name of a product for display. |
*/ |
#import "APLProduct.h" |
NSString *ProductTypeDevice = @"Device"; |
NSString *ProductTypeDesktop = @"Desktop"; |
NSString *ProductTypePortable = @"Portable"; |
@implementation APLProduct |
+ (instancetype)productWithType:(NSString *)type name:(NSString *)name |
{ |
APLProduct *newProduct = [[self alloc] init]; |
newProduct.type = type; |
newProduct.name = name; |
return newProduct; |
} |
+ (NSArray *)deviceTypeNames |
{ |
static NSArray *deviceTypeNames = nil; |
static dispatch_once_t once; |
dispatch_once(&once, ^{ |
deviceTypeNames = @[ProductTypeDevice, ProductTypePortable, ProductTypeDesktop]; |
}); |
return deviceTypeNames; |
} |
+ (NSString *)displayNameForType:(NSString *)type |
{ |
static NSMutableDictionary *deviceTypeDisplayNamesDictionary = nil; |
static dispatch_once_t once; |
dispatch_once(&once, ^{ |
deviceTypeDisplayNamesDictionary = [[NSMutableDictionary alloc] init]; |
for (NSString *deviceType in self.deviceTypeNames) |
{ |
NSString *displayName = NSLocalizedString(deviceType, @"dynamic"); |
deviceTypeDisplayNamesDictionary[deviceType] = displayName; |
} |
}); |
return deviceTypeDisplayNamesDictionary[type]; |
} |
static NSString *NameKey = @"NameKey"; |
static NSString *TypeKey = @"TypeKey"; |
-(id)initWithCoder:(NSCoder *)aDecoder |
{ |
self = [super init]; |
if (self) { |
_name = [aDecoder decodeObjectForKey:NameKey]; |
_type = [aDecoder decodeObjectForKey:TypeKey]; |
} |
return self; |
} |
-(void)encodeWithCoder:(NSCoder *)aCoder |
{ |
[aCoder encodeObject:self.name forKey:NameKey]; |
[aCoder encodeObject:self.type forKey:TypeKey]; |
} |
@end |
Copyright © 2013 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2013-05-23