XLFormOptionsObject.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. +(XLFormOptionsObject *)formOptionsObjectWithValue:(id)value displayText:(NSString *)displayText
  28. {
  29. return [[XLFormOptionsObject alloc] initWithValue:value displayText:displayText];
  30. }
  31. -(instancetype)initWithValue:(id)value displayText:(NSString *)displayText
  32. {
  33. self = [super init];
  34. if (self){
  35. _formValue = value;
  36. _formDisplaytext = displayText;
  37. }
  38. return self;
  39. }
  40. +(XLFormOptionsObject *)formOptionsOptionForValue:(id)value fromOptions:(NSArray *)options
  41. {
  42. for (XLFormOptionsObject * option in options) {
  43. if ([option.formValue isEqual:value]){
  44. return option;
  45. }
  46. }
  47. return nil;
  48. }
  49. +(XLFormOptionsObject *)formOptionsOptionForDisplayText:(NSString *)displayText fromOptions:(NSArray *)options
  50. {
  51. for (XLFormOptionsObject * option in options) {
  52. if ([option.formDisplayText isEqualToString:displayText]){
  53. return option;
  54. }
  55. }
  56. return nil;
  57. }
  58. -(BOOL)isEqual:(id)object
  59. {
  60. if (![[self class] isEqual:[object class]]){
  61. return NO;
  62. }
  63. return [self.formValue isEqual:((XLFormOptionsObject *)object).formValue];
  64. }
  65. #pragma mark - XLFormOptionObject
  66. -(NSString *)formDisplayText
  67. {
  68. return _formDisplaytext;
  69. }
  70. -(id)formValue
  71. {
  72. return _formValue;
  73. }
  74. #pragma mark - NSCoding
  75. -(void)encodeWithCoder:(NSCoder *)encoder
  76. {
  77. [encoder encodeObject:self.formValue
  78. forKey:@"formValue"];
  79. [encoder encodeObject:self.formDisplayText
  80. forKey:@"formDisplayText"];
  81. }
  82. -(instancetype)initWithCoder:(NSCoder *)decoder
  83. {
  84. if ((self=[super init])) {
  85. [self setValue:[decoder decodeObjectForKey:@"formValue"]
  86. forKey:@"formValue"];
  87. [self setValue:[decoder decodeObjectForKey:@"formDisplayText"]
  88. forKey:@"formDisplaytext"];
  89. }
  90. return self;
  91. }
  92. @end