-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFTUtils.h
More file actions
executable file
·215 lines (179 loc) · 7.38 KB
/
Copy pathFTUtils.h
File metadata and controls
executable file
·215 lines (179 loc) · 7.38 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
The MIT License
Copyright (c) 2009 Free Time Studios and Nathan Eror
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
A collection of macros used throughout the FTUtils set of tools.
This file need not be included directly since it's included by the other
pieces of the library. The macros are useful throughout a project, and
it's recommended that you just include the file in your prefix header.
*/
#pragma mark - Version
static const NSUInteger FTUtilsVersion = 010100;
static NSString *const FTUtilsVersionString = @"1.1.0";
#pragma mark - Logging
///---------------------------------------------------------------------------
/// @name Logging Messages to the Console
///---------------------------------------------------------------------------
// FTLOGEXT logging macro from: http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
#ifdef DEBUG
/**
A simple wrapper for `NSLog()` that is automatically removed from release builds.
*/
#define FTLOG(...) NSLog(__VA_ARGS__)
/**
More detailed loogging. Logs the function name and line number after the log message.
*/
#define FTLOGEXT(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
/**
Logs a method call's class and selector.
*/
#define FTLOGCALL FTLOG(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd))
#else
#define FTLOG(...) /* */
#define FTLOGEXT(...) /* */
#define FTLOGCALL /* */
#endif
#pragma mark - Memory Management
///---------------------------------------------------------------------------
/// @name Memory Management
///---------------------------------------------------------------------------
/**
Safely release an objective-c object and set its variable to `nil`.
*/
#define FTRELEASE(_obj) [_obj release], _obj = nil
/**
Safely free a pointer and set its variable to `NULL`.
*/
#define FTFREE(_ptr) if(_ptr != NULL) { free(_ptr); _ptr = NULL; }
#pragma mark - Math
///---------------------------------------------------------------------------
/// @name Math Helpers
///---------------------------------------------------------------------------
/** Convert from degrees to radians */
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
/** Convert from radians to degrees */
#define RADIANS_TO_DEGREES(r) (r * 180 / M_PI)
#pragma mark - Colors
///---------------------------------------------------------------------------
/// @name Creating colors
///---------------------------------------------------------------------------
/**
Create a UIColor with r,g,b values between 0.0 and 1.0.
*/
#define RGBCOLOR(r,g,b) \
[UIColor colorWithRed:r/256.f green:g/256.f blue:b/256.f alpha:1.f]
/**
Create a UIColor with r,g,b,a values between 0.0 and 1.0.
*/
#define RGBACOLOR(r,g,b,a) \
[UIColor colorWithRed:r/256.f green:g/256.f blue:b/256.f alpha:a]
/**
Create a UIColor from a hex value.
For example, `UIColorFromRGB(0xFF0000)` creates a `UIColor` object representing
the color red.
*/
#define UIColorFromRGB(rgbValue) \
[UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:1.0]
/**
Create a UIColor with an alpha value from a hex value.
For example, `UIColorFromRGBA(0xFF0000, .5)` creates a `UIColor` object
representing a half-transparent red.
*/
#define UIColorFromRGBA(rgbValue, alphaValue) \
[UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:alphaValue]
#pragma mark - Delegates
///---------------------------------------------------------------------------
/// @name Calling Delegates
///---------------------------------------------------------------------------
/**
Call a delegate method if the selector exists.
*/
#define FT_CALL_DELEGATE(_delegate, _selector) \
do { \
id _theDelegate = _delegate; \
if(_theDelegate != nil && [_theDelegate respondsToSelector:_selector]) { \
[_theDelegate performSelector:_selector]; \
} \
} while(0);
/**
Call a delegate method that accepts one argument if the selector exists.
*/
#define FT_CALL_DELEGATE_WITH_ARG(_delegate, _selector, _argument) \
do { \
id _theDelegate = _delegate; \
if(_theDelegate != nil && [_theDelegate respondsToSelector:_selector]) { \
[_theDelegate performSelector:_selector withObject:_argument]; \
} \
} while(0);
/**
Call a delegate method that accepts two arguments if the selector exists.
*/
#define FT_CALL_DELEGATE_WITH_ARGS(_delegate, _selector, _arg1, _arg2) \
do { \
id _theDelegate = _delegate; \
if(_theDelegate != nil && [_theDelegate respondsToSelector:_selector]) { \
[_theDelegate performSelector:_selector withObject:_arg1 withObject:_arg2]; \
} \
} while(0);
#pragma mark - File System
///---------------------------------------------------------------------------
/// @name Working with the filesystem
///---------------------------------------------------------------------------
/**
Get the full path for a file name in the documents directory.
@param filename the name of the file _(the file need not exist yet)_.
@return The full path to the filename.
*/
static inline NSString *FTPathForFileInDocumentsDirectory(NSString *filename) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:filename];
return path;
}
#pragma mark - Core Data
///---------------------------------------------------------------------------
/// @name Working with the Core Data
///---------------------------------------------------------------------------
/**
Save a Core Data `NSManagedObjectContext` and pretty print any errors.
*/
#define FT_SAVE_MOC(_ft_moc) \
do { \
NSError* _ft_save_error; \
if(![_ft_moc save:&_ft_save_error]) { \
FTLOG(@"Failed to save to data store: %@", [_ft_save_error localizedDescription]); \
NSArray* _ft_detailedErrors = [[_ft_save_error userInfo] objectForKey:NSDetailedErrorsKey]; \
if(_ft_detailedErrors != nil && [_ft_detailedErrors count] > 0) { \
for(NSError* _ft_detailedError in _ft_detailedErrors) { \
FTLOG(@"DetailedError: %@", [_ft_detailedError userInfo]); \
} \
} \
else { \
FTLOG(@"%@", [_ft_save_error userInfo]); \
} \
} \
} while(0);