-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBWiFiProxyInfo.m
64 lines (50 loc) · 2.1 KB
/
MBWiFiProxyInfo.m
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
#import "MBWiFiProxyInfo.h"
@interface NSDictionary<KeyType, ObjectType> (Getters)
- (nullable NSString *)stringForKeySafely:(nullable KeyType)key;
- (nullable NSNumber *)numberForKeySafely:(nullable KeyType)key;
@end
@implementation NSDictionary (Getters)
- (NSString *)stringForKeySafely:(id)key {
id string = [self objectForKey:key];
if ([string isKindOfClass:[NSString class]]) { return string; }
if ([string isKindOfClass:[NSNumber class]]) { return [NSString stringWithFormat:@"%@", string]; }
return nil;
}
- (NSNumber *)numberForKeySafely:(id)key {
id number = [self objectForKey:key];
if ([number isKindOfClass:[NSNumber class]]) { return number; }
if ([number isKindOfClass:[NSString class]]) { return [self numberFromString:(NSString *)number]; }
return nil;
}
- (NSNumber *)numberFromString:(NSString *)string {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
return [formatter numberFromString:string];
}
@end
@implementation MBWiFiProxyInfo
+ (instancetype)infoWithServer:(NSString *)server port:(NSNumber *)port username:(nullable NSString *)username password:(nullable NSString *)password {
return [[self alloc] initWithServer:server port:port username:username password:password];
}
+ (instancetype)infoFromDictionary:(NSDictionary *)dictionary {
return [[self alloc] initFromDictionary:dictionary];
}
- (instancetype)initWithServer:(NSString *)server port:(NSNumber *)port username:(nullable NSString *)username password:(nullable NSString *)password {
if (self = [super init]) {
_server = server;
_port = port;
_username = username;
_password = password;
}
return self;
}
- (instancetype)initFromDictionary:(NSDictionary *)dictionary {
if (self = [super init]) {
_server = [dictionary stringForKeySafely:@"server"];
_port = [dictionary numberForKeySafely:@"port"];
_username = [dictionary stringForKeySafely:@"username"];
_password = [dictionary stringForKeySafely:@"password"];
}
return self;
}
@end