1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// JTOptionsEntity.m
// JobTalk
//
// Created by Xummer on 14-5-22.
// Copyright (c) 2014年 BST. All rights reserved.
//
#import "JTOptionsEntity.h"
#define OPTION_ENTITY_KEY @"name"
#define OPTION_ENTITY_LOCAL_KEY @"login"
@interface JTOptionsEntity ()
@end
@implementation JTOptionsEntity
+ (NSString *)lowcaseAndDeleteWhitespace:(NSString *)str {
return [[str lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
}
+ (NSInteger)indexOfOptionStr:(NSString *)option inOptions:(NSArray *)options {
return [options indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
// NSString *tmpObj = [[self class] lowcaseAndDeleteWhitespace:obj];
// NSString *tmpOpt = [[self class] lowcaseAndDeleteWhitespace:option];
if ([obj isEqualToString:option]) {
return YES;
}
else {
return NO;
}
}];
}
+ (JTOptionsEntity *)entityWithOptions:(NSArray *)options andSelectedIndex:(NSInteger)sIndex {
return [[self alloc] initWithOptions:options andSelectedIndex:sIndex];
}
+ (JTOptionsEntity *)entityWithOptions:(NSArray *)options
defaultOptionIndex:(NSInteger)dIndex
andSelectedOption:(NSString *)option
{
if (dIndex < 0 || dIndex >= [options count]) {
dIndex = 0;
}
NSArray *arrDis = nil;
if ([[options firstObject] isKindOfClass:[NSString class]]) {
arrDis = options;
}
else {
arrDis = [options valueForKeyPath:OPTION_ENTITY_LOCAL_KEY];
}
NSInteger sInd = [[self class] indexOfOptionStr:option inOptions:arrDis];
sInd = sInd == NSNotFound ? dIndex : sInd;
return [[self class] entityWithOptions:options andSelectedIndex:sInd];
}
+ (JTOptionsEntity *)entityWithOptions:(NSArray *)options andSelectedOption:(NSString *)option {
NSArray *arrDis = nil;
if ([[options firstObject] isKindOfClass:[NSString class]]) {
arrDis = options;
}
else {
arrDis = [options valueForKeyPath:OPTION_ENTITY_LOCAL_KEY];
}
NSInteger sInd = [[self class] indexOfOptionStr:option inOptions:arrDis];
sInd = sInd == NSNotFound ? 0 : sInd;
return [[self class] entityWithOptions:options andSelectedIndex:sInd];
}
- (id)initWithOptions:(NSArray *)options andSelectedIndex:(NSInteger)sIndex {
self = [super init];
if (!self) {
return nil;
}
self.optionsArray = options;
if ([[options firstObject] isKindOfClass:[NSString class]]) {
self.m_arrDisplayStr = options;
}
else {
self.m_arrDisplayStr = [options valueForKeyPath:OPTION_ENTITY_LOCAL_KEY];
}
if (sIndex >= 0 && sIndex < [options count]) {
self.selectedIndex = sIndex;
}
return self;
}
- (id)getSelectedOption {
if (_selectedIndex >= 0 && _selectedIndex < [_optionsArray count]) {
return _optionsArray[ _selectedIndex ];
}
else {
return _defaultOption;
}
}
- (id)getSelectedOptionForCommit {
return [[self class] lowcaseAndDeleteWhitespace:[self getSelectedOption]];
}
- (NSInteger)indexOfOptionStr:(NSString *)option {
return [[self class] indexOfOptionStr:option inOptions:_m_arrDisplayStr];
}
@end