XLFormOptionsObject.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // XLFormOptionsObject.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "XLFormOptionsObject.h"
  26. @implementation XLFormOptionsObject
  27. {
  28. NSString * _formDisplaytext;
  29. id _formValue;
  30. }
  31. +(XLFormOptionsObject *)formOptionsObjectWithValue:(id)value displayText:(NSString *)displayText
  32. {
  33. return [[XLFormOptionsObject alloc] initWithValue:value displayText:displayText];
  34. }
  35. -(instancetype)initWithValue:(id)value displayText:(NSString *)displayText
  36. {
  37. self = [super init];
  38. if (self){
  39. _formValue = value;
  40. _formDisplaytext = displayText;
  41. }
  42. return self;
  43. }
  44. +(XLFormOptionsObject *)formOptionsOptionForValue:(id)value fromOptions:(NSArray *)options
  45. {
  46. for (XLFormOptionsObject * option in options) {
  47. if ([option.formValue isEqual:value]){
  48. return option;
  49. }
  50. }
  51. return nil;
  52. }
  53. +(XLFormOptionsObject *)formOptionsOptionForDisplayText:(NSString *)displayText fromOptions:(NSArray *)options
  54. {
  55. for (XLFormOptionsObject * option in options) {
  56. if ([option.formDisplayText isEqualToString:displayText]){
  57. return option;
  58. }
  59. }
  60. return nil;
  61. }
  62. -(BOOL)isEqual:(id)object
  63. {
  64. if (![[self class] isEqual:[object class]]){
  65. return NO;
  66. }
  67. return [self.formValue isEqual:((XLFormOptionsObject *)object).formValue];
  68. }
  69. #pragma mark - XLFormOptionObject
  70. -(NSString *)formDisplayText
  71. {
  72. return _formDisplaytext;
  73. }
  74. -(id)formValue
  75. {
  76. return _formValue;
  77. }
  78. #pragma mark - NSCoding
  79. -(void)encodeWithCoder:(NSCoder *)encoder
  80. {
  81. [encoder encodeObject:self.formValue
  82. forKey:@"formValue"];
  83. [encoder encodeObject:self.formDisplayText
  84. forKey:@"formDisplayText"];
  85. }
  86. -(instancetype)initWithCoder:(NSCoder *)decoder
  87. {
  88. if ((self=[super init])) {
  89. [self setValue:[decoder decodeObjectForKey:@"formValue"]
  90. forKey:@"formValue"];
  91. [self setValue:[decoder decodeObjectForKey:@"formDisplayText"]
  92. forKey:@"formDisplaytext"];
  93. }
  94. return self;
  95. }
  96. @end