Skip to content

Commit ac6357d

Browse files
author
Jasper
committed
增加Util
1 parent a16aa52 commit ac6357d

File tree

15 files changed

+354
-7
lines changed

15 files changed

+354
-7
lines changed

CYUtilProject/.DS_Store

2 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// CYExceptionUtils.h
3+
// CYUtilProject
4+
//
5+
// Created by xn011644 on 5/13/16.
6+
// Copyright © 2016 Charry. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
typedef void (^CYUncaughtExceptionsHandler)(NSException *exception);
12+
13+
@interface CYExceptionUtils : NSObject
14+
15+
+ (void)registerUncaughtExceptionsHandler:(CYUncaughtExceptionsHandler)handler;
16+
17+
@end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// CYExceptionUtils.m
3+
// CYUtilProject
4+
//
5+
// Created by xn011644 on 5/13/16.
6+
// Copyright © 2016 Charry. All rights reserved.
7+
//
8+
9+
#import "CYExceptionUtils.h"
10+
11+
@interface CYExceptionUtils()
12+
13+
@property (nonatomic, copy) CYUncaughtExceptionsHandler uncaughtExceptionHandler;
14+
15+
@end
16+
17+
@implementation CYExceptionUtils
18+
19+
void CYUncaughtExceptionHandler(NSException *exception) {
20+
21+
CYUncaughtExceptionsHandler handler = [CYExceptionUtils sharedInstance].uncaughtExceptionHandler;
22+
if (handler) {
23+
24+
handler(exception);
25+
}
26+
}
27+
28+
+ (void)registerUncaughtExceptionsHandler:(CYUncaughtExceptionsHandler)handler {
29+
30+
[CYExceptionUtils sharedInstance].uncaughtExceptionHandler = handler;
31+
32+
static dispatch_once_t onceToken;
33+
dispatch_once(&onceToken, ^{
34+
35+
NSSetUncaughtExceptionHandler(&CYUncaughtExceptionHandler);
36+
});
37+
}
38+
39+
+ (instancetype)sharedInstance {
40+
41+
static CYExceptionUtils *utils = nil;
42+
static dispatch_once_t onceToken;
43+
dispatch_once(&onceToken, ^{
44+
45+
utils = [[CYExceptionUtils alloc] init];
46+
});
47+
return utils;
48+
}
49+
50+
@end

CYUtilProject/CYFoundationUtils/NSArray+CYJson.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@
1010

1111
@interface NSArray (CYJson)
1212

13+
// to json
1314
- (NSString *)cy_jsonString;
1415
- (NSData *)cy_jsonData;
1516

17+
// create array from json
1618
+ (NSArray *)cy_arrayFromJsonString:(NSString *)jsonString;
1719
+ (NSArray *)cy_arrayFromJsonData:(NSData *)jsonData;
1820

21+
// write to file as json string
22+
- (BOOL)writeToFileAsJson:(NSString *)filePath
23+
automically:(BOOL)useAuxiliaryFile;
24+
25+
- (BOOL)writeToFileAsJson:(NSString *)filePath
26+
atomically:(BOOL)useAuxiliaryFile
27+
error:(NSError **)error;
28+
1929
@end

CYUtilProject/CYFoundationUtils/NSArray+CYJson.m

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ - (NSData *)cy_jsonData {
3434

3535
return data;
3636
} else {
37-
38-
// CYDLog(@"parse json error : %@", error);
37+
3938
return nil;
4039
}
4140
}
4241

42+
// create array from json string
4343
+ (NSArray *)cy_arrayFromJsonString:(NSString *)jsonString {
4444

4545
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
4646
return [self cy_arrayFromJsonData:jsonData];
4747
}
4848

49+
// create array from json Data
4950
+ (NSArray *)cy_arrayFromJsonData:(NSData *)jsonData {
5051

5152
NSError *error = nil;
@@ -58,10 +59,31 @@ + (NSArray *)cy_arrayFromJsonData:(NSData *)jsonData {
5859
return jsonObject;
5960
} else {
6061

61-
// CYDLog(@"parse json error : %@", error);
6262
return nil;
6363
}
6464

6565
}
6666

67+
#pragma mark - writeToFile
68+
// write to file as json string
69+
- (BOOL)writeToFileAsJson:(NSString *)filePath automically:(BOOL)useAuxiliaryFile {
70+
71+
return [self writeToFileAsJson:filePath
72+
atomically:useAuxiliaryFile
73+
error:nil];
74+
}
75+
76+
- (BOOL)writeToFileAsJson:(NSString *)filePath atomically:(BOOL)useAuxiliaryFile error:(NSError **)error {
77+
78+
if (!filePath) {
79+
80+
return NO;
81+
}
82+
NSString *jsonString = [self cy_jsonString];
83+
return [jsonString writeToFile:filePath
84+
atomically:useAuxiliaryFile
85+
encoding:NSUTF8StringEncoding
86+
error:error];
87+
}
88+
6789
@end

CYUtilProject/CYFoundationUtils/NSDictionary+CYJson.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@
1010

1111
@interface NSDictionary (CYJson)
1212

13+
// to json
1314
- (NSString *)cy_jsonString;
1415
- (NSData *)cy_jsonData;
1516

17+
// create dictionary from json
1618
+ (NSDictionary *)cy_dictionaryFromJsonString:(NSString *)jsonString;
1719
+ (NSDictionary *)cy_dictionaryFromJsonData:(NSData *)jsonData;
1820

21+
// write to file as json string
22+
- (BOOL)writeToFileAsJson:(NSString *)filePath
23+
automically:(BOOL)useAuxiliaryFile;
24+
- (BOOL)writeToFileAsJson:(NSString *)filePath
25+
atomically:(BOOL)useAuxiliaryFile
26+
error:(NSError **)error;
27+
1928
@end

CYUtilProject/CYFoundationUtils/NSDictionary+CYJson.m

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
@implementation NSDictionary (CYJson)
1212

13+
#pragma mark - toJson
14+
// dictionary to json string
1315
- (NSString *)cy_jsonString {
1416

1517
NSData *data = [self cy_jsonData];
@@ -24,28 +26,32 @@ - (NSString *)cy_jsonString {
2426
}
2527
}
2628

29+
// dictionary to json data
2730
- (NSData *)cy_jsonData {
2831

2932
NSError *error = nil;
3033
NSData *data = [NSJSONSerialization dataWithJSONObject:self
3134
options:0
3235
error:&error];
36+
3337
if (!error) {
3438

3539
return data;
3640
} else {
37-
38-
// CYDLog(@"parse json error : %@", error);
41+
3942
return nil;
4043
}
4144
}
4245

46+
#pragma mark - fromJson
47+
// create dictionary from json string
4348
+ (NSDictionary *)cy_dictionaryFromJsonString:(NSString *)jsonString {
4449

4550
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
4651
return [self cy_dictionaryFromJsonData:jsonData];
4752
}
4853

54+
// create dictionary from json Data
4955
+ (NSDictionary *)cy_dictionaryFromJsonData:(NSData *)jsonData {
5056

5157
NSError *error = nil;
@@ -57,11 +63,34 @@ + (NSDictionary *)cy_dictionaryFromJsonData:(NSData *)jsonData {
5763

5864
return jsonObject;
5965
} else {
60-
61-
// CYDLog(@"parse json error : %@", error);
66+
6267
return nil;
6368
}
6469

6570
}
6671

72+
73+
#pragma mark - writeToFile
74+
// write to file as json string
75+
- (BOOL)writeToFileAsJson:(NSString *)filePath automically:(BOOL)useAuxiliaryFile {
76+
77+
return [self writeToFileAsJson:filePath
78+
atomically:useAuxiliaryFile
79+
error:nil];
80+
}
81+
82+
- (BOOL)writeToFileAsJson:(NSString *)filePath atomically:(BOOL)useAuxiliaryFile error:(NSError **)error {
83+
84+
if (!filePath) {
85+
86+
return NO;
87+
}
88+
NSString *jsonString = [self cy_jsonString];
89+
return [jsonString writeToFile:filePath
90+
atomically:useAuxiliaryFile
91+
encoding:NSUTF8StringEncoding
92+
error:error];
93+
}
94+
95+
6796
@end

CYUtilProject/CYIPUtils/CYIPUtils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// CYIPUtils.h
3+
// CYUtilProject
4+
//
5+
// Created by xn011644 on 5/16/16.
6+
// Copyright © 2016 Charry. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface CYIPUtils : NSObject
12+
13+
+ (NSString *)IPAddress;
14+
15+
@end

CYUtilProject/CYIPUtils/CYIPUtils.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// CYIPUtils.m
3+
// CYUtilProject
4+
//
5+
// Created by xn011644 on 5/16/16.
6+
// Copyright © 2016 Charry. All rights reserved.
7+
//
8+
9+
#import <ifaddrs.h>
10+
#import <arpa/inet.h>
11+
12+
#import "CYIPUtils.h"
13+
14+
@implementation CYIPUtils
15+
16+
+ (NSString *)IPAddress {
17+
18+
NSString *address = @"error";
19+
struct ifaddrs *interfaces = NULL;
20+
struct ifaddrs *temp_addr = NULL;
21+
int success = 0;
22+
23+
// retrieve the current interfaces - returns 0 on success
24+
success = getifaddrs(&interfaces);
25+
if (success == 0) {
26+
// Loop through linked list of interfaces
27+
temp_addr = interfaces;
28+
while (temp_addr != NULL) {
29+
if( temp_addr->ifa_addr->sa_family == AF_INET) {
30+
// Check if interface is en0 which is the wifi connection on the iPhone
31+
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
32+
// Get NSString from C String
33+
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
34+
}
35+
}
36+
37+
temp_addr = temp_addr->ifa_next;
38+
}
39+
}
40+
41+
// Free memory
42+
freeifaddrs(interfaces);
43+
44+
return address;
45+
}
46+
47+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// UIImage+CYUtils.h
3+
// CYUtilProject
4+
//
5+
// Created by xn011644 on 5/11/16.
6+
// Copyright © 2016 Charry. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIImage (CYUtils)
12+
13+
// 缩放到固定size,不一定等比
14+
- (UIImage *)imageByScaleImageToSize:(CGSize)size;
15+
16+
// 等比缩放到固定宽度或高度
17+
- (UIImage *)imageByScaleImageWidthTo:(CGFloat)width;
18+
- (UIImage *)imageByScaleImageHeightTo:(CGFloat)height;
19+
20+
// get resizable image from file, use imageNamed:
21+
+ (UIImage *)resizableImageWithName:(NSString *)name
22+
capInsets:(UIEdgeInsets)capInsets;
23+
24+
// get origin rendering mode image, use imageNamed:
25+
+ (UIImage *)originalRenderingModeImageWithName:(NSString *)name;
26+
27+
@end

0 commit comments

Comments
 (0)