Skip to content

Commit e000386

Browse files
committed
add error obj beside return result in block.
1 parent 8c5d5dc commit e000386

File tree

4 files changed

+126
-29
lines changed

4 files changed

+126
-29
lines changed

CSVParser/ViewController.m

+24-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
@interface ViewController ()
1313
@property (strong) NSArray *array;
14+
@property (weak, nonatomic) IBOutlet UILabel *numberOfEntriesLabel;
1415
@end
1516

1617
@implementation ViewController
@@ -22,34 +23,47 @@ - (void)viewDidLoad
2223
[super viewDidLoad];
2324
// Do any additional setup after loading the view, typically from a nib.
2425

25-
NSString *file = [[NSBundle mainBundle] pathForResource:@"USPresident Wikipedia" ofType:@"csv"];
26+
// NSString *file = [[NSBundle mainBundle] pathForResource:@"USPresident Wikipedia" ofType:@"csv"];
2627

2728
// For Testing
2829

2930
// [CSVParser parseCSVIntoArrayOfDictionariesFromFile:file
3031
// withSeparatedCharacterString:@","
3132
// quoteCharacterString:nil
32-
// withBlock:^(NSArray *array) {
33+
// withBlock:^(NSArray *array, NSError *error) {
3334
// self.array = array;
3435
//// NSLog(@"%@", self.array);
36+
// self.numberOfEntriesLabel.text = [NSString stringWithFormat:@"%d", array.count];
3537
// }];
3638

37-
[CSVParser parseCSVIntoArrayOfArraysFromFile:file
38-
withSeparatedCharacterString:@","
39-
quoteCharacterString:nil
40-
withBlock:^(NSArray *array) {
41-
self.array = array;
42-
// NSLog(@"%@", self.array);
43-
}];
39+
// NSLog(@"%@", self.array);
40+
41+
// [CSVParser parseCSVIntoArrayOfArraysFromFile:file
42+
// withSeparatedCharacterString:@","
43+
// quoteCharacterString:nil
44+
// withBlock:^(NSArray *array, NSError *error) {
45+
// self.array = array;
46+
//// NSLog(@"%@", self.array);
47+
// }];
4448
}
4549

4650
- (IBAction)showArray:(id)sender
4751
{
48-
NSLog(@"%@", self.array);
52+
// NSLog(@"%@", self.array);
53+
NSString *file = [[NSBundle mainBundle] pathForResource:@"USPresident Wikipedia" ofType:@"csv"];
54+
[CSVParser parseCSVIntoArrayOfDictionariesFromFile:file
55+
withSeparatedCharacterString:@","
56+
quoteCharacterString:nil
57+
withBlock:^(NSArray *array, NSError *error) {
58+
self.array = array;
59+
self.numberOfEntriesLabel.text = [NSString stringWithFormat:@"%d", array.count];
60+
}];
61+
4962
}
5063

5164
- (void)viewDidUnload
5265
{
66+
[self setNumberOfEntriesLabel:nil];
5367
[super viewDidUnload];
5468
// Release any retained subviews of the main view.
5569
}

CSVParser/en.lproj/ViewController.xib

+92-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="8.00">
33
<data>
4-
<int key="IBDocument.SystemTarget">1296</int>
5-
<string key="IBDocument.SystemVersion">11E53</string>
6-
<string key="IBDocument.InterfaceBuilderVersion">2549</string>
7-
<string key="IBDocument.AppKitVersion">1138.47</string>
8-
<string key="IBDocument.HIToolboxVersion">569.00</string>
4+
<int key="IBDocument.SystemTarget">1552</int>
5+
<string key="IBDocument.SystemVersion">12E55</string>
6+
<string key="IBDocument.InterfaceBuilderVersion">3084</string>
7+
<string key="IBDocument.AppKitVersion">1187.39</string>
8+
<string key="IBDocument.HIToolboxVersion">626.00</string>
99
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
1010
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
11-
<string key="NS.object.0">1498</string>
11+
<string key="NS.object.0">2083</string>
1212
</object>
1313
<array key="IBDocument.IntegratedClassDependencies">
1414
<string>IBProxyObject</string>
1515
<string>IBUIButton</string>
16+
<string>IBUILabel</string>
1617
<string>IBUIView</string>
1718
</array>
1819
<array key="IBDocument.PluginDependencies">
@@ -40,7 +41,7 @@
4041
<int key="NSvFlags">292</int>
4142
<string key="NSFrame">{{70, 403}, {180, 37}}</string>
4243
<reference key="NSSuperview" ref="774585933"/>
43-
<reference key="NSNextKeyView"/>
44+
<reference key="NSWindow"/>
4445
<string key="NSReuseIdentifierKey">_NS:9</string>
4546
<bool key="IBUIOpaque">NO</bool>
4647
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
@@ -70,9 +71,41 @@
7071
<int key="NSfFlags">16</int>
7172
</object>
7273
</object>
74+
<object class="IBUILabel" id="221142650">
75+
<reference key="NSNextResponder" ref="774585933"/>
76+
<int key="NSvFlags">292</int>
77+
<string key="NSFrame">{{139, 20}, {42, 21}}</string>
78+
<reference key="NSSuperview" ref="774585933"/>
79+
<reference key="NSWindow"/>
80+
<string key="NSReuseIdentifierKey">_NS:9</string>
81+
<bool key="IBUIOpaque">NO</bool>
82+
<bool key="IBUIClipsSubviews">YES</bool>
83+
<int key="IBUIContentMode">7</int>
84+
<bool key="IBUIUserInteractionEnabled">NO</bool>
85+
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
86+
<string key="IBUIText">Label</string>
87+
<object class="NSColor" key="IBUITextColor">
88+
<int key="NSColorSpace">1</int>
89+
<bytes key="NSRGB">MCAwIDAAA</bytes>
90+
<string key="IBUIColorCocoaTouchKeyPath">darkTextColor</string>
91+
</object>
92+
<nil key="IBUIHighlightedColor"/>
93+
<int key="IBUIBaselineAdjustment">0</int>
94+
<object class="IBUIFontDescription" key="IBUIFontDescription">
95+
<int key="type">1</int>
96+
<double key="pointSize">17</double>
97+
</object>
98+
<object class="NSFont" key="IBUIFont">
99+
<string key="NSName">Helvetica</string>
100+
<double key="NSSize">17</double>
101+
<int key="NSfFlags">16</int>
102+
</object>
103+
<bool key="IBUIAdjustsFontSizeToFit">NO</bool>
104+
</object>
73105
</array>
74106
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
75107
<reference key="NSSuperview"/>
108+
<reference key="NSWindow"/>
76109
<reference key="NSNextKeyView" ref="996184961"/>
77110
<object class="NSColor" key="IBUIBackgroundColor">
78111
<int key="NSColorSpace">3</int>
@@ -96,6 +129,14 @@
96129
</object>
97130
<int key="connectionID">7</int>
98131
</object>
132+
<object class="IBConnectionRecord">
133+
<object class="IBCocoaTouchOutletConnection" key="connection">
134+
<string key="label">numberOfEntriesLabel</string>
135+
<reference key="source" ref="372490531"/>
136+
<reference key="destination" ref="221142650"/>
137+
</object>
138+
<int key="connectionID">11</int>
139+
</object>
99140
<object class="IBConnectionRecord">
100141
<object class="IBCocoaTouchEventConnection" key="connection">
101142
<string key="label">showArray:</string>
@@ -130,6 +171,7 @@
130171
<reference key="object" ref="774585933"/>
131172
<array class="NSMutableArray" key="children">
132173
<reference ref="996184961"/>
174+
<reference ref="221142650"/>
133175
</array>
134176
<reference key="parent" ref="0"/>
135177
</object>
@@ -138,31 +180,70 @@
138180
<reference key="object" ref="996184961"/>
139181
<reference key="parent" ref="774585933"/>
140182
</object>
183+
<object class="IBObjectRecord">
184+
<int key="objectID">10</int>
185+
<reference key="object" ref="221142650"/>
186+
<reference key="parent" ref="774585933"/>
187+
</object>
141188
</array>
142189
</object>
143190
<dictionary class="NSMutableDictionary" key="flattenedProperties">
144191
<string key="-1.CustomClassName">ViewController</string>
145192
<string key="-1.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
146193
<string key="-2.CustomClassName">UIResponder</string>
147194
<string key="-2.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
195+
<string key="10.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
148196
<string key="6.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
149197
<string key="8.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
150198
</dictionary>
151199
<dictionary class="NSMutableDictionary" key="unlocalizedProperties"/>
152200
<nil key="activeLocalization"/>
153201
<dictionary class="NSMutableDictionary" key="localizations"/>
154202
<nil key="sourceID"/>
155-
<int key="maxID">9</int>
203+
<int key="maxID">11</int>
204+
</object>
205+
<object class="IBClassDescriber" key="IBDocument.Classes">
206+
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
207+
<object class="IBPartialClassDescription">
208+
<string key="className">ViewController</string>
209+
<string key="superclassName">UIViewController</string>
210+
<object class="NSMutableDictionary" key="actions">
211+
<string key="NS.key.0">showArray:</string>
212+
<string key="NS.object.0">id</string>
213+
</object>
214+
<object class="NSMutableDictionary" key="actionInfosByName">
215+
<string key="NS.key.0">showArray:</string>
216+
<object class="IBActionInfo" key="NS.object.0">
217+
<string key="name">showArray:</string>
218+
<string key="candidateClassName">id</string>
219+
</object>
220+
</object>
221+
<object class="NSMutableDictionary" key="outlets">
222+
<string key="NS.key.0">numberOfEntriesLabel</string>
223+
<string key="NS.object.0">UILabel</string>
224+
</object>
225+
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
226+
<string key="NS.key.0">numberOfEntriesLabel</string>
227+
<object class="IBToOneOutletInfo" key="NS.object.0">
228+
<string key="name">numberOfEntriesLabel</string>
229+
<string key="candidateClassName">UILabel</string>
230+
</object>
231+
</object>
232+
<object class="IBClassDescriptionSource" key="sourceIdentifier">
233+
<string key="majorKey">IBProjectSource</string>
234+
<string key="minorKey">./Classes/ViewController.h</string>
235+
</object>
236+
</object>
237+
</array>
156238
</object>
157-
<object class="IBClassDescriber" key="IBDocument.Classes"/>
158239
<int key="IBDocument.localizationMode">0</int>
159240
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
160241
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
161242
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS</string>
162-
<real value="1296" key="NS.object.0"/>
243+
<real value="1552" key="NS.object.0"/>
163244
</object>
164245
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
165246
<int key="IBDocument.defaultPropertyAccessControl">3</int>
166-
<string key="IBCocoaTouchPluginVersion">1498</string>
247+
<string key="IBCocoaTouchPluginVersion">2083</string>
167248
</data>
168249
</archive>

Parser/CSVParser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
+ (void)parseCSVIntoArrayOfDictionariesFromFile:(NSString *)path
2222
withSeparatedCharacterString:(NSString *)character
2323
quoteCharacterString:(NSString *)quote
24-
withBlock:(void (^)(NSArray *array))block;
24+
withBlock:(void (^)(NSArray *array, NSError *error))block;
2525

2626
+ (void)parseCSVIntoArrayOfArraysFromFile:(NSString *)path
2727
withSeparatedCharacterString:(NSString *)character
2828
quoteCharacterString:(NSString *)quote
29-
withBlock:(void (^)(NSArray *array))block;
29+
withBlock:(void (^)(NSArray *array, NSError *error))block;
3030

3131
@end

Parser/CSVParser.m

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ + (NSArray *)trimComponents:(NSArray *)array withCharacters:(NSString *)characte
2626
+ (void)parseCSVIntoArrayOfDictionariesFromFile:(NSString *)path
2727
withSeparatedCharacterString:(NSString *)character
2828
quoteCharacterString:(NSString *)quote
29-
withBlock:(void (^)(NSArray *array))block
29+
withBlock:(void (^)(NSArray *array, NSError *error))block
3030
{
3131
dispatch_queue_t callerQueue = dispatch_get_current_queue();
3232
dispatch_queue_t queue = dispatch_queue_create("parseQueue", NULL);
3333
dispatch_async(queue, ^{
34+
NSError *err = nil;
3435
NSMutableArray *mutArray = [[NSMutableArray alloc] init];
35-
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
36+
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
3637
if (!content) return;
3738
NSArray *rows = [content componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\r"]];
3839
NSString *trimStr = (quote != nil) ? [quote stringByAppendingString:@"\n\r "] : @"\n\r ";
@@ -45,7 +46,7 @@ + (void)parseCSVIntoArrayOfDictionariesFromFile:(NSString *)path
4546
}
4647
if (block) {
4748
dispatch_async(callerQueue, ^{
48-
block(mutArray);
49+
block(mutArray, err);
4950
});
5051
}
5152
dispatch_release(callerQueue);
@@ -56,14 +57,15 @@ + (void)parseCSVIntoArrayOfDictionariesFromFile:(NSString *)path
5657
+ (void)parseCSVIntoArrayOfArraysFromFile:(NSString *)path
5758
withSeparatedCharacterString:(NSString *)character
5859
quoteCharacterString:(NSString *)quote
59-
withBlock:(void (^)(NSArray *array))block
60+
withBlock:(void (^)(NSArray *array, NSError *error))block
6061
{
6162
dispatch_queue_t callerQueue = dispatch_get_current_queue();
6263
dispatch_queue_t queue = dispatch_queue_create("parseQueue", NULL);
6364

6465
dispatch_async(queue, ^{
66+
NSError *err = nil;
6567
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
66-
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
68+
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
6769
if (!content) return;
6870
NSArray *rows = [content componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\r"]];
6971
NSString *trimStr = (quote != nil) ? [quote stringByAppendingString:@"\n\r "] : @"\n\r ";
@@ -73,7 +75,7 @@ + (void)parseCSVIntoArrayOfArraysFromFile:(NSString *)path
7375
}];
7476
if (block) {
7577
dispatch_async(callerQueue, ^{
76-
block(mutableArray);
78+
block(mutableArray, err);
7779
});
7880
}
7981
dispatch_release(callerQueue);

0 commit comments

Comments
 (0)