Gowhich

Durban's Blog

最近做app,涉及到关于数据的存储,对于远程数据的请求,有个问题就是,对于同样的请求,不做重复的操作,但是又能够保证,获取的数据在本地,并且即使的进行解析,之前是NSdata,这样的话,转换数据的话,就要涉及到一个中间步骤,就是将数据转换为json格式,在转换为NSdictionary,解析的过程会出现的可能性,导致数据不能及时响应,最近几次的闪退,估计是这个原因,于是做了调整,闪退的现象果然概率不再频繁。

这次主要是分享一下,修改后的封装类,主要是在里面添加了几个关于存储NSDictionary的方法

代码托管在githubhttps://github.com/zhangda89/IOS-StorageData

StorageData.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//
// StorageData.m
// xunYi7
//
// Created by david on 13-6-28.
// Copyright (c) 2013年 david. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "StorageData.h"
#import "xunYi7AppDelegate.h"


@implementation StorageData


-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"开始结didReceiveData搜数据");
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"开始结didReceiveResponse搜数据");
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"didFailWithError");
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"connectionDidFinishLoading");
}

+(NSMutableData *)remoteFetchData:(NSString *)dataUrl{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",[self md5:dataUrl]]];

if([xunYi7AppDelegate isReachable]){
NSURL *url = [[NSURL alloc] initWithString:dataUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];

NSURLResponse *response = [[NSURLResponse alloc] init];
NSError *receiveDataError = [[NSError alloc] init];

NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&receiveDataError];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

if(receivedData != nil){
if([self storageDataToFile:receivedData fileName:currentDataFilePath]){
[self removeDirectory];
}
}

return receivedData;
}else{
[xunYi7AppDelegate showNetworkMessage];
}

return nil;
}

+(NSMutableData *)localFetchData:(NSString *)dataUrl{

NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",[self md5:dataUrl]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",[self md5:dataUrl]]];

NSMutableData *localData = [self fromFilenamePathFetchLocalData:currentDataFilePath];

if(localData != nil){//本地数据
return localData;

}else{//远程获取数据

NSMutableData *receivedData = [self remoteFetchData:dataUrl];

if(receivedData != nil){
if([self storageDataToFile:receivedData fileName:currentDataFilePath]){
// NSLog(@"保存成功");
[self removeDirectory];
}else{
// NSLog(@"保存失败");
}
}else{
if((localData = [self fromFilenamePathFetchLocalData:yesterdayDataFilePath]) != nil){
return localData;
}
}
return receivedData;
}
return nil;
}

#pragma mark - 获取本地数据的Dcitionary格式-艺人排行版
+(NSDictionary *) localFetchPersonRankDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 Dictionary 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 Dictionary 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSDictionary *personInfo = [personDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:personInfo fileName:currentDataFilePath]){
// NSLog(@"远程数据存储成功");
return personInfo;
[self removeDirectory];
}else{
// NSLog(@"远程数据存储失败");s
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 获取本地数据的Dcitionary格式-关注列表
+(NSDictionary *) localFetchAttentionListDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 localFetchAttentionListDictionayData 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 localFetchAttentionListDictionayData 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *attentionDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSDictionary *attentionData = [attentionDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:attentionData fileName:currentDataFilePath]){
// NSLog(@"localFetchAttentionListDictionayData 远程数据存储成功");
return attentionData;
[self removeDirectory];
}else{
return attentionData;
// NSLog(@"localFetchAttentionListDictionayData 远程数据存储失败");
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 获取远程数据的Dcitionary格式-关注列表
+(NSDictionary *) remoteFetchAttentionListDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *attentionDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSDictionary *attentionData = [attentionDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:attentionData fileName:currentDataFilePath]){
// NSLog(@"localFetchAttentionListDictionayData 远程数据存储成功");
return attentionData;
[self removeDirectory];
}else{
return attentionData;
// NSLog(@"localFetchAttentionListDictionayData 远程数据存储失败");
}
}

return nil;
}

#pragma mark - 获取本地数据的Dcitionary格式-机会列表
+(NSDictionary *) localFetchChanceListDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 localFetchChanceListDictionayData 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 localFetchChanceListDictionayData 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *chanceDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSMutableDictionary *chanceData = [chanceDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:chanceData fileName:currentDataFilePath]){
// NSLog(@"localFetchChanceListDictionayData 远程数据存储成功");
return chanceData;
[self removeDirectory];
}else{
return chanceData;
// NSLog(@"localFetchChanceListDictionayData 远程数据存储失败");
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 获取本地数据的Dcitionary格式-电视剧排行榜
+(NSDictionary *) localFetchTeleplayRankDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 localFetchTeleplayRankDictionayData 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 localFetchTeleplayRankDictionayData 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *teleplayDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSMutableDictionary *teleplayData = [teleplayDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:teleplayData fileName:currentDataFilePath]){
// NSLog(@"localFetchTeleplayRankDictionayData 远程数据存储成功");
return teleplayData;
[self removeDirectory];
}else{
return teleplayData;
// NSLog(@"localFetchTeleplayRankDictionayData 远程数据存储失败");
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 获取本地数据的Dcitionary格式-艺人签单排行榜
+(NSDictionary *) localFetchStarkCheckDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 localFetchStarkCheckDictionayData 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 localFetchStarkCheckDictionayData 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *starkCheckDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSMutableDictionary *starkCheckData = [starkCheckDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:starkCheckData fileName:currentDataFilePath]){
// NSLog(@"localFetchStarkCheckDictionayData 远程数据存储成功");
return starkCheckData;
[self removeDirectory];
}else{
return starkCheckData;
// NSLog(@"localFetchStarkCheckDictionayData 远程数据存储失败");
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 获取本地数据的Dcitionary格式-hotTeleplay
+(NSDictionary *) localFetchHotTeleplayDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 localFetchHotTeleplayDictionayData 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 localFetchHotTeleplayDictionayData 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *starkCheckDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSMutableDictionary *starkCheckData = [starkCheckDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:starkCheckData fileName:currentDataFilePath]){
// NSLog(@"localFetchHotTeleplayDictionayData 远程数据存储成功");
return starkCheckData;
[self removeDirectory];
}else{
return starkCheckData;
// NSLog(@"localFetchHotTeleplayDictionayData 远程数据存储失败");
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 获取远程数据的Dcitionary格式-找演员
+(NSDictionary *) remoteFetchFindPerformersDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

//远程获取数据
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSMutableDictionary *searchPersonResult = [personDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:searchPersonResult fileName:currentDataFilePath]){
// NSLog(@"localFetchFindPerformersDictionayData 远程数据存储成功");
return searchPersonResult;
[self removeDirectory];
}else{
return searchPersonResult;
// NSLog(@"localFetchFindPerformersDictionayData 远程数据存储失败");
}
}
return nil;
}


+(NSDictionary *) localFetchFindPerformersDictionayData:(NSString *)dataUrlString{
NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];

//创建目录
currentDataFilePath = [self createDirectory:currentDataFilePath];

currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];
yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-dictionary.plist",[self md5:dataUrlString]]];

NSDictionary *localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:currentDataFilePath];

if(localDictionaryData != nil){//本地数据
// NSLog(@"本地数据 localFetchFindPerformersDictionayData 数据");
return localDictionaryData;
}else{//远程获取数据
// NSLog(@"远程获取数据 localFetchFindPerformersDictionayData 数据");
NSMutableData *receivedData = [self remoteFetchData:dataUrlString];

if(receivedData != nil){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSError *jsonError = [[NSError alloc] init];
NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:receivedData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSMutableDictionary *searchPersonResult = [personDictionary objectForKey:@"data"];

if([self storageDictionaryDataToFile:searchPersonResult fileName:currentDataFilePath]){
// NSLog(@"localFetchFindPerformersDictionayData 远程数据存储成功");
return searchPersonResult;
[self removeDirectory];
}else{
return searchPersonResult;
// NSLog(@"localFetchFindPerformersDictionayData 远程数据存储失败");
}
}else{
if((localDictionaryData = [self fromFilenamePathFetchLocalDictionaryData:yesterdayDataFilePath]) != nil){
return localDictionaryData;
}
}
}
return nil;
}

#pragma mark - 存储Dictionary格式的数据到本地-艺人排行版
+(void) savePersonRankDictionaryToLocal:(NSMutableDictionary *)data dataUrlString:(NSString *)urlString{

}

//md5加密字符串
+(NSString *)md5:(NSString *)str{
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
//上传图片存储
+(void) saveUploadImage:(UIImage *)image withName:(NSString *)imageName{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error;

// 获取沙盒目录
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
fullPath = [fullPath stringByAppendingPathComponent:@"tmpImage"];
if(![fileManager fileExistsAtPath:fullPath]){
[fileManager createDirectoryAtPath:fullPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}

fullPath = [fullPath stringByAppendingPathComponent:imageName];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

// 将图片写入文件
[imageData writeToFile:fullPath atomically:NO];
}

//上传图片删除
+(void) removeUploadImage:(UIImage *)image withName:(NSString *)imageName{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error;

// 获取沙盒目录
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
fullPath = [fullPath stringByAppendingPathComponent:@"tmpImage"];
if(![fileManager fileExistsAtPath:fullPath]){
[fileManager removeItemAtPath:fullPath error:&error];
}
}

//获取存储的图片
+(NSString *)fetchUploadImagePath:(NSString *)imageName{
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
fullPath = [fullPath stringByAppendingPathComponent:@"tmpImage"];
fullPath = [fullPath stringByAppendingPathComponent:imageName];
return fullPath;
}

//判断文件是否存在
+(NSString *)isFileExists:(NSString *)fullpath{
NSFileManager *fileManager = [[NSFileManager alloc] init];
if([fileManager fileExistsAtPath:fullpath]){
return fullpath;
}
return nil;
}

//数据存储
//+(void)

//获取存储文件的目录
+(NSString *)dataPath{
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//并给文件起个文件名
NSString *filePathDerectory = [paths objectAtIndex:0];

return filePathDerectory;
}

//获取指定文件的数据
+(NSMutableData *)fromFilenamePathFetchLocalData:(NSString *)filename{
//保存数据到指定文件中
NSFileManager *fileManager = [[NSFileManager alloc] init];
if([fileManager fileExistsAtPath:filename]){
NSData *data = [fileManager contentsAtPath:filename];
return [data mutableCopy];
}

return nil;
}

+(NSDictionary *)fromFilenamePathFetchLocalDictionaryData:(NSString *)filename{

NSDictionary *dicData = [[NSDictionary alloc] initWithContentsOfFile:filename];

if(dicData != nil){
return dicData;
}


return nil;
}

//存储数据到指定文件
+(BOOL) storageDataToFile:(NSData *)data fileName:(NSString *)fileName{
//保存数据到指定文件中
NSFileManager *fileManager = [[NSFileManager alloc] init];
if([fileManager createFileAtPath:fileName contents:data attributes:nil]){
return YES;
}else{
return NO;
}
}

#pragma mark - 存储Dictionary格式的数据
+(BOOL) storageDictionaryDataToFile:(NSDictionary *)dicData fileName:(NSString *)fileName{
//保存数据到指定文件中
if([dicData writeToFile:fileName atomically:YES]){
return YES;
}else{
return NO;
}
}

//删除文件
+(void) deleteFile:(NSString *)fileName{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error;
[fileManager removeItemAtPath:fileName error:&error];
}

//获取今天的日期
+(NSString *) fetchTodayDate{
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
return [dateFormatter stringFromDate:currentDate];
}

//获取前天的日期
+(NSString *) fetchYesterdayBeforeDate{
NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-(2 * (24 * 60 * 60))];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
return [dateFormatter stringFromDate:yesterdayDate];
}

//获取存储文件的数据

//创建文件

//创建目录
+(NSString *) createDirectory:(NSString *)directoryName{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error;
if(![fileManager fileExistsAtPath:directoryName]){
[fileManager createDirectoryAtPath:directoryName
withIntermediateDirectories:YES
attributes:nil
error:&error];
if(error == nil){
return directoryName;
}else{
return directoryName;
}
}else{
return directoryName;
}
}
//删除文件
+(void) removeFile:(NSString *)filePath{
NSError *error;

NSFileManager *fileManager = [[NSFileManager alloc] init];
if([fileManager fileExistsAtPath:filePath]){
[fileManager removeItemAtPath:filePath error:&error];
}
if(error){
NSLog(@"error = %@",error);
}
}

//删除目录
+(void) removeDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *removeDirectoryPath = [documentsPath stringByAppendingPathComponent:[self fetchYesterdayBeforeDate]];
NSError *error;

NSFileManager *fileManager = [[NSFileManager alloc] init];
if([fileManager fileExistsAtPath:removeDirectoryPath]){
[fileManager removeItemAtPath:removeDirectoryPath error:&error];
}
if(error){
NSLog(@"error = %@",error);
}
}
@end

  1. 如何横/竖分屏打开新文件
1
2
3
4
5
:sp filename
:split filename
:vsp filename
:vsplit filename
:sview filename ->只读分屏打开文件

从命令行直接打开多个文件且是分屏

1
2
vim -On file1, file2 ...  ->垂直分屏
vim -on file1, file2 ... ->水平分屏

其中n为分几个屏

  1. 如何横/竖分屏打开当前文件
1
2
ctrl+W s
ctrl+W v
  1. 如何切换分屏
1
2
3
ctrl+w w
ctrl+w h,j,k,l
ctrl+w 上下左右键头
  1. 如何关闭分屏
1
2
3
4
ctrl+W c 关闭当前窗口
ctrl+w q 关闭当前窗口,若只有一个分屏且退出vim
:only 仅保留当前分屏
:hide 关闭当前分屏
  1. 如何调整分屏的大小
1
2
3
4
ctrl+w = 所有分屏都统一高度
ctrl+w + 增加高度
ctrl+w - 减少高度
10 ctrl+w + 增加10行高度
  1. 如何移动分屏
1
ctrl+W H,J,K,L

7.多标签

多个标签间进行切换时向右切换gt,向左切换用gT

在编辑的时候想增加一个标签就可以:tabnew filename

1
2
3
4
5
6
7
8
9
:tabc       关闭当前的tab

:tabo 关闭所有其他的tab

:tabs 查看所有打开的tab

:tabp 前一个

:tabn 后一个

关于MACVIM的配置,我这里只介绍几个网站的找到的配置文件

第一个是我在使用的:

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
syntax on " 自动语法高亮
colorscheme molokai " 设定配色方案
set number " 显示行号
set cursorline " 突出显示当前行
set ruler " 打开状态栏标尺
set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4
set softtabstop=4 " 使得按退格键时可以一次删掉 4 个空格
set tabstop=4 " 设定 tab 长度为 4
set nobackup " 覆盖文件时不备份
set autochdir " 自动切换当前目录为当前文件所在的目录
filetype plugin indent on " 开启插件
set backupcopy=yes " 设置备份时的行为为覆盖
set ignorecase smartcase " 搜索时忽略大小写,但在有一个或以上大写字母时仍保持对大小写敏感
set nowrapscan " 禁止在搜索到文件两端时重新搜索
set incsearch " 输入搜索内容时就显示搜索结果
set hlsearch " 搜索时高亮显示被找到的文本
set noerrorbells " 关闭错误信息响铃
set novisualbell " 关闭使用可视响铃代替呼叫
set t_vb= " 置空错误铃声的终端代码
" set showmatch " 插入括号时,短暂地跳转到匹配的对应括号
" set matchtime=2 " 短暂跳转到匹配括号的时间
set magic " 设置魔术
set hidden " 允许在有未保存的修改时切换缓冲区,此时的修改由 vim 负责保存
set guioptions-=T " 隐藏工具栏
set guioptions-=m " 隐藏菜单栏
set smartindent " 开启新行时使用智能自动缩进
set backspace=indent,eol,start
" 不设定在插入状态无法用退格键和 Delete 键删除回车符
set cmdheight=1 " 设定命令行的行数为 1
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息
set foldenable " 开始折叠
set foldmethod=syntax " 设置语法折叠
set foldcolumn=0 " 设置折叠区域的宽度
setlocal foldlevel=1 " 设置折叠层数为
" set foldclose=all " 设置为自动关闭折叠
" nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 用空格键来开关折叠


" return OS type, eg: windows, or linux, mac, et.st..
function! MySys()
if has("win16") || has("win32") || has("win64") || has("win95")
return "windows"
elseif has("unix")
return "linux"
endif
endfunction

" 用户目录变量$VIMFILES
if MySys() == "windows"
let $VIMFILES = $VIM.'/vimfiles'
elseif MySys() == "linux"
let $VIMFILES = $HOME.'/.vim'
endif

" 设定doc文档目录
let helptags=$VIMFILES.'/doc'

" 设置字体 以及中文支持
if has("win32")
set guifont=Inconsolata:h12:cANSI
endif

" 配置多语言环境
if has("multi_byte")
" UTF-8 编码
set encoding=utf-8
set termencoding=utf-8
set formatoptions+=mM
set fencs=utf-8,gbk

if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
set ambiwidth=double
endif

if has("win32")
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
language messages zh_CN.utf-8
endif
else
echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"
endif

" Buffers操作快捷方式!
nnoremap <C-RETURN> :bnext<CR>
nnoremap <C-S-RETURN> :bprevious<CR>

" Tab操作快捷方式!
nnoremap <C-TAB> :tabnext<CR>
nnoremap <C-S-TAB> :tabprev<CR>

"关于tab的快捷键
" map tn :tabnext<cr>
" map tp :tabprevious<cr>
" map td :tabnew .<cr>
" map te :tabedit
" map tc :tabclose<cr>

"窗口分割时,进行切换的按键热键需要连接两次,比如从下方窗口移动
"光标到上方窗口,需要<c-w><c-w>k,非常麻烦,现在重映射为<c-k>,切换的
"时候会变得非常方便.
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

"一些不错的映射转换语法(如果在一个文件中混合了不同语言时有用)
nnoremap <leader>1 :set filetype=xhtml<CR>
nnoremap <leader>2 :set filetype=css<CR>
nnoremap <leader>3 :set filetype=javascript<CR>
nnoremap <leader>4 :set filetype=php<CR>

" set fileformats=unix,dos,mac
" nmap <leader>fd :se fileformat=dos<CR>
" nmap <leader>fu :se fileformat=unix<CR>

" use Ctrl+[l|n|p|cc] to list|next|previous|jump to count the result
" map <C-x>l <ESC>:cl<CR>
" map <C-x>n <ESC>:cn<CR>
" map <C-x>p <ESC>:cp<CR>
" map <C-x>c <ESC>:cc<CR>


" 让 Tohtml 产生有 CSS 语法的 html
" syntax/2html.vim,可以用:runtime! syntax/2html.vim
let html_use_css=1

" Python 文件的一般设置,比如不要 tab 等
autocmd FileType python set tabstop=4 shiftwidth=4 expandtab
autocmd FileType python map <F12> :!python %<CR>

" 选中状态下 Ctrl+c 复制
vmap <C-c> "+y

" 打开javascript折叠
let b:javascript_fold=1
" 打开javascript对dom、html和css的支持
let javascript_enable_domhtmlcss=1
" 设置字典 ~/.vim/dict/文件的路径
autocmd filetype javascript set dictionary=$VIMFILES/dict/javascript.dict
autocmd filetype css set dictionary=$VIMFILES/dict/css.dict
autocmd filetype php set dictionary=$VIMFILES/dict/php.dict

"-----------------------------------------------------------------
" plugin - bufexplorer.vim Buffers切换
" \be 全屏方式查看全部打开的文件列表
" \bv 左右方式查看 \bs 上下方式查看
"-----------------------------------------------------------------


"-----------------------------------------------------------------
" plugin - taglist.vim 查看函数列表,需要ctags程序
" F4 打开隐藏taglist窗口
"-----------------------------------------------------------------
if MySys() == "windows" " 设定windows系统中ctags程序的位置
let Tlist_Ctags_Cmd = '"'.$VIMRUNTIME.'/ctags.exe"'
elseif MySys() == "linux" " 设定windows系统中ctags程序的位置
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
endif
nnoremap <silent><F4> :TlistToggle<CR>
let Tlist_Show_One_File = 1 " 不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow = 1 " 如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1 " 在右侧窗口中显示taglist窗口
let Tlist_File_Fold_Auto_Close=1 " 自动折叠当前非编辑文件的方法列表
let Tlist_Auto_Open = 0
let Tlist_Auto_Update = 1
let Tlist_Hightlight_Tag_On_BufEnter = 1
let Tlist_Enable_Fold_Column = 0
let Tlist_Process_File_Always = 1
let Tlist_Display_Prototype = 0
let Tlist_Compact_Format = 1


"-----------------------------------------------------------------
" plugin - mark.vim 给各种tags标记不同的颜色,便于观看调式的插件。
" \m mark or unmark the word under (or before) the cursor
" \r manually input a regular expression. 用于搜索.
" \n clear this mark (i.e. the mark under the cursor), or clear all highlighted marks .
" \* 当前MarkWord的下一个 \# 当前MarkWord的上一个
" \/ 所有MarkWords的下一个 \? 所有MarkWords的上一个
"-----------------------------------------------------------------


"-----------------------------------------------------------------
" plugin - NERD_tree.vim 以树状方式浏览系统中的文件和目录
" :ERDtree 打开NERD_tree :NERDtreeClose 关闭NERD_tree
" o 打开关闭文件或者目录 t 在标签页中打开
" T 在后台标签页中打开 ! 执行此文件
" p 到上层目录 P 到根目录
" K 到第一个节点 J 到最后一个节点
" u 打开上层目录 m 显示文件系统菜单(添加、删除、移动操作)
" r 递归刷新当前目录 R 递归刷新当前根目录
"-----------------------------------------------------------------
" F3 NERDTree 切换
map <F3> :NERDTreeToggle<CR>
imap <F3> <ESC>:NERDTreeToggle<CR>


"-----------------------------------------------------------------
" plugin - NERD_commenter.vim 注释代码用的,
" [count],cc 光标以下count行逐行添加注释(7,cc)
" [count],cu 光标以下count行逐行取消注释(7,cu)
" [count],cm 光标以下count行尝试添加块注释(7,cm)
" ,cA 在行尾插入 ,并且进入插入模式。 这个命令方便写注释。
" 注:count参数可选,无则默认为选中行或当前行
"-----------------------------------------------------------------
let NERDSpaceDelims=1 " 让注释符与语句之间留一个空格
let NERDCompactSexyComs=1 " 多行注释时样子更好看


"-----------------------------------------------------------------
" plugin - DoxygenToolkit.vim 由注释生成文档,并且能够快速生成函数标准注释
"-----------------------------------------------------------------
let g:DoxygenToolkit_authorName="Asins - asinsimple AT gmail DOT com"
let g:DoxygenToolkit_briefTag_funcName="yes"
map <leader>da :DoxAuthor<CR>
map <leader>df :Dox<CR>
map <leader>db :DoxBlock<CR>
map <leader>dc a <LEFT><LEFT><LEFT>


"-----------------------------------------------------------------
" plugin – ZenCoding.vim 很酷的插件,HTML代码生成
" 插件最新版:http://github.com/mattn/zencoding-vim
" 常用命令可看:http://nootn.com/blog/Tool/23/
"-----------------------------------------------------------------


"-----------------------------------------------------------------
" plugin – checksyntax.vim JavaScript常见语法错误检查
" 默认快捷方式为 F5
"-----------------------------------------------------------------
let g:checksyntax_auto = 0 " 不自动检查


"-----------------------------------------------------------------
" plugin - NeoComplCache.vim 自动补全插件
"-----------------------------------------------------------------
let g:AutoComplPop_NotEnableAtStartup = 1
let g:NeoComplCache_EnableAtStartup = 1
let g:NeoComplCache_SmartCase = 1
let g:NeoComplCache_TagsAutoUpdate = 1
let g:NeoComplCache_EnableInfo = 1
let g:NeoComplCache_EnableCamelCaseCompletion = 1
let g:NeoComplCache_MinSyntaxLength = 3
let g:NeoComplCache_EnableSkipCompletion = 1
let g:NeoComplCache_SkipInputTime = '0.5'
let g:NeoComplCache_SnippetsDir = $VIMFILES.'/snippets'
" <TAB> completion.
inoremap <expr><TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
" snippets expand key
imap <silent> <C-e> <Plug>(neocomplcache_snippets_expand)
smap <silent> <C-e> <Plug>(neocomplcache_snippets_expand)


"-----------------------------------------------------------------
" plugin - matchit.vim 对%命令进行扩展使得能在嵌套标签和语句之间跳转
" % 正向匹配 g% 反向匹配
" [% 定位块首 ]% 定位块尾
"-----------------------------------------------------------------


"-----------------------------------------------------------------
" plugin - vcscommand.vim 对%命令进行扩展使得能在嵌套标签和语句之间跳转
" SVN/git管理工具
"-----------------------------------------------------------------


"-----------------------------------------------------------------
" plugin – a.vim
"-----------------------------------------------------------------
参考的是:http://blog.sina.com.cn/s/blog_5a6efa330101cumx.html这篇文章

第二个是,据说是最全的一个,也是最好的一个,不过我没有使用,我用了第一个后感觉第一个可以复制命令,第二个就没有了,看来里面还是有区别的,抽时间看看

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Maintainer: amix the lucky stiff
" http://amix.dk - xx@xx
"
" Version: 3.6 - 25/08/10 14:40:30
"
" Blog_post:
" http://amix.dk/blog/post/19486#The-ultimate-vim-configuration-vimrc
" Syntax_highlighted:
" http://amix.dk/vim/vimrc.html
" Raw_version:
" http://amix.dk/vim/vimrc.txt
"
" How_to_Install_on_Unix:
" $ mkdir ~/.vim_runtime
" $ svn co svn://orangoo.com/vim ~/.vim_runtime
" $ cat ~/.vim_runtime/install.sh
" $ sh ~/.vim_runtime/install.sh <system>
" <sytem> can be `mac`, `linux` or `windows`
"
" How_to_Upgrade:
" $ svn update ~/.vim_runtime
"
" Sections:
" -> General
" -> VIM user interface
" -> Colors and Fonts
" -> Files and backups
" -> Text, tab and indent related
" -> Visual mode related
" -> Command mode related
" -> Moving around, tabs and buffers
" -> Statusline
" -> Parenthesis/bracket expanding
" -> General Abbrevs
" -> Editing mappings
"
" -> Cope
" -> Minibuffer plugin
" -> Omni complete functions
" -> Python section
" -> JavaScript section
"
"
" Plugins_Included:
" > minibufexpl.vim - http://www.vim.org/scripts/script.php?script_id=159
" Makes it easy to get an overview of buffers:
" info -> :e ~/.vim_runtime/plugin/minibufexpl.vim
"
" > bufexplorer - http://www.vim.org/scripts/script.php?script_id=42
" Makes it easy to switch between buffers:
" info -> :help bufExplorer
"
" > yankring.vim - http://www.vim.org/scripts/script.php?script_id=1234
" Emacs's killring, useful when using the clipboard:
" info -> :help yankring
"
" > surround.vim - http://www.vim.org/scripts/script.php?script_id=1697
" Makes it easy to work with surrounding text:
" info -> :help surround
"
" > snipMate.vim - http://www.vim.org/scripts/script.php?script_id=2540
" Snippets for many languages (similar to TextMate's):
" info -> :help snipMate
"
" > mru.vim - http://www.vim.org/scripts/script.php?script_id=521
" Plugin to manage Most Recently Used (MRU) files:
" info -> :e ~/.vim_runtime/plugin/mru.vim
"
" > Command-T - http://www.vim.org/scripts/script.php?script_id=3025
" Command-T plug-in provides an extremely fast, intuitive mechanism for opening filesa:
" info -> :help CommandT
" screencast and web-help -> http://amix.dk/blog/post/19501
"
"
" Revisions:
" > 3.6: Added lots of stuff (colors, Command-T, Vim 7.3 persistent undo etc.)
" > 3.5: Paste mode is now shown in status line if you are in paste mode
" > 3.4: Added mru.vim
" > 3.3: Added syntax highlighting for Mako mako.vim
" > 3.2: Turned on python_highlight_all for better syntax
" highlighting for Python
" > 3.1: Added revisions ;) and bufexplorer.vim
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Sets how many lines of history VIM has to remember
set history=700

" Enable filetype plugin
filetype plugin on
filetype indent on

" Set to auto read when a file is changed from the outside
set autoread

" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
let g:mapleader = ","

" Fast saving
nmap <leader>w :w!<cr>

" Fast editing of the .vimrc
map <leader>e :e! ~/.vim_runtime/vimrc<cr>

" When vimrc is edited, reload it
autocmd! bufwritepost vimrc source ~/.vim_runtime/vimrc


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => VIM user interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set 7 lines to the curors - when moving vertical..
set so=7

set wildmenu "Turn on WiLd menu

set ruler "Always show current position

set cmdheight=2 "The commandbar height

set hid "Change buffer - without saving

" Set backspace config
set backspace=eol,start,indent
set whichwrap+=<,>,h,l

set ignorecase "搜索时忽略大小写
set smartcase

set hlsearch "开启搜索高亮

set incsearch "输入搜索字符串的同时进行搜索
set nolazyredraw "Don't redraw while executing macros

set magic "Set magic on, for regular expressions

set showmatch "Show matching bracets when text indicator is over them
set mat=2 "How many tenths of a second to blink

" No sound on errors
set noerrorbells
set novisualbell
set t_vb=
set tm=500


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Colors and Fonts
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax enable "开启代码高亮

func! MySys()
return "mac"
endfunc

" Set font according to system
if MySys() == "mac"
set gfn=SimSun:h16
set shell=/bin/bash
elseif MySys() == "windows"
set gfn=Bitstream\ Vera\ Sans\ Mono:h10
elseif MySys() == "linux"
set gfn=Monospace\ 10
set shell=/bin/bash
endif

if has("gui_running")
set guioptions-=T
set t_Co=256
colorscheme laphp
endif

set number

set encoding=utf-8
set fileencodings=utf-8,chinese,latin-1

source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim

try
lang en_US
catch
endtry

set ffs=unix,dos,mac "Default file types


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Files, backups and undo
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Turn backup off, since most stuff is in SVN, git anyway...
set nobackup
set nowb
set noswapfile

"Persistent undo
try
if MySys() == "windows"
set undodir=C:\Windows\Temp
else
set undodir=~/.vim_runtime/undodir
endif

set undofile
catch
endtry


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text, tab and indent related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=4
set tabstop=4
set smarttab

set lbr
set tw=500

set ai "Auto indent
set si "Smart indet
set wrap "Wrap lines


""""""""""""""""""""""""""""""
" => Visual mode related
""""""""""""""""""""""""""""""
" Really useful!
" In visual mode when you press * or # to search for the current selection
vnoremap <silent> * :call VisualSearch('f')<CR>
vnoremap <silent> # :call VisualSearch('b')<CR>

" When you press gv you vimgrep after the selected text
vnoremap <silent> gv :call VisualSearch('gv')<CR>
map <leader>g :vimgrep // **/*.<left><left><left><left><left><left><left>


function! CmdLine(str)
exe "menu Foo.Bar :" . a:str
emenu Foo.Bar
unmenu Foo
endfunction

" From an idea by Michael Naumann
function! VisualSearch(direction) range
let l:saved_reg = @"
execute "normal! vgvy"

let l:pattern = escape(@", '\\/.*$^~[]')
let l:pattern = substitute(l:pattern, "\n{1}quot;, "", "")

if a:direction == 'b'
execute "normal ?" . l:pattern . "^M"
elseif a:direction == 'gv'
call CmdLine("vimgrep " . '/'. l:pattern . '/' . ' **/*.')
elseif a:direction == 'f'
execute "normal /" . l:pattern . "^M"
endif

let @/ = l:pattern
let @" = l:saved_reg
endfunction



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Command mode related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Smart mappings on the command line
cno $h e ~/
cno $d e ~/Desktop/
cno $j e ./
cno $c e <C-\>eCurrentFileDir("e")<cr>

" $q is super useful when browsing on the command line
cno $q <C-\>eDeleteTillSlash()<cr>

" Bash like keys for the command line
cnoremap <C-A> <Home>
cnoremap <C-E> <End>
cnoremap <C-K> <C-U>

cnoremap <C-P> <Up>
cnoremap <C-N> <Down>

" Useful on some European keyboards
map ½ $
imap ½ $
vmap ½ $
cmap ½ $


func! Cwd()
let cwd = getcwd()
return "e " . cwd
endfunc

func! DeleteTillSlash()
let g:cmd = getcmdline()
if MySys() == "linux" || MySys() == "mac"
let g:cmd_edited = substitute(g:cmd, "\\(.*\[/\]\\).*", "\\1", "")
else
let g:cmd_edited = substitute(g:cmd, "\\(.*\[\\\\]\\).*", "\\1", "")
endif
if g:cmd == g:cmd_edited
if MySys() == "linux" || MySys() == "mac"
let g:cmd_edited = substitute(g:cmd, "\\(.*\[/\]\\).*/", "\\1", "")
else
let g:cmd_edited = substitute(g:cmd, "\\(.*\[\\\\\]\\).*\[\\\\\]", "\\1", "")
endif
endif
return g:cmd_edited
endfunc

func! CurrentFileDir(cmd)
return a:cmd . " " . expand("%:p:h") . "/"
endfunc


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around, tabs and buffers
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Map space to / (search) and c-space to ? (backgwards search)
map <space> /
map <c-space> ?
map <silent> <leader><cr> :noh<cr>

" Smart way to move btw. windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

" Close the current buffer
map <leader>bd :Bclose<cr>

" Close all the buffers
map <leader>ba :1,300 bd!<cr>

" Use the arrows to something usefull
map <right> :bn<cr>
map <left> :bp<cr>

" Tab configuration
map <leader>tn :tabnew<cr>
map <leader>te :tabedit
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove

" When pressing <leader>cd switch to the directory of the open buffer
map <leader>cd :cd %:p:h<cr>


command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
let l:currentBufNum = bufnr("%")
let l:alternateBufNum = bufnr("#")

if buflisted(l:alternateBufNum)
buffer #
else
bnext
endif

if bufnr("%") == l:currentBufNum
new
endif

if buflisted(l:currentBufNum)
execute("bdelete! ".l:currentBufNum)
endif
endfunction

" Specify the behavior when switching between buffers
try
set switchbuf=usetab
set stal=2
catch
endtry


""""""""""""""""""""""""""""""
" => Statusline
""""""""""""""""""""""""""""""
" Always hide the statusline
set laststatus=2

" Format the statusline
set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L:%c


function! CurDir()
let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")
return curdir
endfunction

function! HasPaste()
if &paste
return 'PASTE MODE '
else
return ''
endif
endfunction


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parenthesis/bracket expanding
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vnoremap $1 <esc>`>a)<esc>`<i(<esc>
vnoremap $2 <esc>`>a]<esc>`<i[<esc>
vnoremap $3 <esc>`>a}<esc>`<i{<esc>
vnoremap $ <esc>`>a"<esc>`<i"<esc>
vnoremap $q <esc>`>a'<esc>`<i'<esc>
vnoremap $e <esc>`>a"<esc>`<i"<esc>

" Map auto complete of (, ", ', [
inoremap $1 ()<esc>i
inoremap $2 []<esc>i
inoremap $3 {}<esc>i
inoremap $4 {<esc>o}<esc>O
inoremap $q ''<esc>i
inoremap $e ""<esc>i
inoremap $t <><esc>i


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General Abbrevs
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
iab xdate <c-r>=strftime("%d/%m/%y %H:%M:%S")<cr>


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Editing mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Remap VIM 0
map 0 ^

"Move a line of text using ALT+[jk] or Comamnd+[jk] on mac
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z

if MySys() == "mac"
nmap <D-j> <M-j>
nmap <D-k> <M-k>
vmap <D-j> <M-j>
vmap <D-k> <M-k>
endif

"Delete trailing white space, useful for Python ;)
func! DeleteTrailingWS()
exe "normal mz"
%s/\s\+$//ge
exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()

set guitablabel=%t


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => short Tab Label
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set guitablabel=%{ShortTabLabel()}
function ShortTabLabel ()
let bufnrlist = tabpagebuflist (v:lnum)
let label = bufname (bufnrlist[tabpagewinnr (v:lnum) -1])
let filename = fnamemodify (label, ':t')
return filename
endfunction


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Cope
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Do :help cope if you are unsure what cope is. It's super useful!
map <leader>cc :botright cope<cr>
map <leader>n :cn<cr>
map <leader>p :cp<cr>


""""""""""""""""""""""""""""""
" => bufExplorer plugin
""""""""""""""""""""""""""""""
let g:bufExplorerDefaultHelp=0
let g:bufExplorerShowRelativePath=1
map <leader>o :BufExplorer<cr>


""""""""""""""""""""""""""""""
" => Minibuffer plugin
""""""""""""""""""""""""""""""
let g:miniBufExplModSelTarget = 1
let g:miniBufExplorerMoreThanOne = 2
let g:miniBufExplModSelTarget = 0
let g:miniBufExplUseSingleClick = 1
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplVSplit = 25
let g:miniBufExplSplitBelow=1

let g:bufExplorerSortBy = "name"

autocmd BufRead,BufNew :call UMiniBufExplorer

map <leader>u :TMiniBufExplorer<cr>


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Omni complete functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd FileType css set omnifunc=csscomplete#CompleteCSS


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Spell checking
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Pressing ,ss will toggle and untoggle spell checking
map <leader>ss :setlocal spell!<cr>

"Shortcuts using <leader>
map <leader>sn ]s
map <leader>sp [s
map <leader>sa zg
map <leader>s? z=


""""""""""""""""""""""""""""""
" => Python section
""""""""""""""""""""""""""""""
let python_highlight_all = 1
au FileType python syn keyword pythonDecorator True None False self

au BufNewFile,BufRead *.jinja set syntax=htmljinja
au BufNewFile,BufRead *.mako set ft=mako

au FileType python inoremap <buffer> $r return
au FileType python inoremap <buffer> $i import
au FileType python inoremap <buffer> $p print
au FileType python inoremap <buffer> $f #--- PH ----------------------------------------------<esc>FP2xi
au FileType python map <buffer> <leader>1 /class
au FileType python map <buffer> <leader>2 /def
au FileType python map <buffer> <leader>C ?class
au FileType python map <buffer> <leader>D ?def


""""""""""""""""""""""""""""""
" => JavaScript section
"""""""""""""""""""""""""""""""
au FileType javascript call JavaScriptFold()
au FileType javascript setl fen
au FileType javascript setl nocindent

au FileType javascript imap <c-t> AJS.log();<esc>hi
au FileType javascript imap <c-a> alert();<esc>hi

au FileType javascript inoremap <buffer> $r return
au FileType javascript inoremap <buffer> $f //--- PH ----------------------------------------------<esc>FP2xi

function! JavaScriptFold()
setl foldmethod=syntax
setl foldlevelstart=1
syn region foldBraces start=/{/ end=/}/ transparent fold keepend extend

function! FoldText()
return substitute(getline(v:foldstart), '{.*', '{...}', '')
endfunction
setl foldtext=FoldText()
endfunction


""""""""""""""""""""""""""""""
" => MRU plugin
""""""""""""""""""""""""""""""
let MRU_Max_Entries = 400
map <leader>f :MRU<CR>


""""""""""""""""""""""""""""""
" => Command-T
""""""""""""""""""""""""""""""
let g:CommandTMaxHeight = 15
set wildignore+=*.o,*.obj,.git,*.pyc
noremap <leader>j :CommandT<cr>
noremap <leader>y :CommandTFlush<cr>


""""""""""""""""""""""""""""""
" => Vim grep
""""""""""""""""""""""""""""""
let Grep_Skip_Dirs = 'RCS CVS SCCS .svn generated'
set grepprg=/bin/grep\ -nH



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => MISC
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remove the Windows ^M - when the encodings gets messed up
noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm

"Quickly open a buffer for scripbble
map <leader>q :e ~/buffer<cr>
au BufRead,BufNewFile ~/buffer iab <buffer> xh1 ===========================================

map <leader>pp :setlocal paste!<cr>

map <leader>bb :cd ..<cr>


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => tag list
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let Tlist_Ctags_Cmd="/usr/local/bin/ctags"

let Tlist_Use_Right_Window=1

参考的是:http://blog.csdn.net/luochuan/article/details/7252916

还有一个是使用命令行安装的,呵呵我不怎么喜欢这样的,不过还是贴到下面,给喜欢的朋友们

1
sudo apt-get install vim-scripts

然后想安装什么插件都可以输入以下命令:

1
2
3
4
5
vim-addons install taglist   
vim-addons install supertab
vim-addons install cscope
vim-addons install winmanager
vim-addons install tags

然后修改相应的配置就行了,如果找不到~/.vimrc可以在vi /etc/vim/vimrc中修改

先来看看代码:

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
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
var range = 50; //距下边界长度/单位px
var elemt = 500; //插入元素高度/单位px
var maxnum = 3; //设置加载最多次数
var num = 0;
$(window).scroll(function(){
var srollPos = $(window).scrollTop(); //滚动条距顶部距离(页面超出窗口的高度)
var dbHiht = $("body").height(); //页面(约等于窗体)高度/单位px
var main = $("#try"); //主体元素
var mainHiht = main.height(); //主体元素高度/单位px

if((srollPos + dbHiht) >= (mainHiht-range) && num != maxnum){
main.append("<div style='height:"+elemt+"' >hello world"+srollPos+"---"+num+"</div>");
num++;
}
});
});
</script>
</head>
<body>
<div id="try" style="height:960px">
<div id="follow">this is a scroll test;<br/> 页面下拉自动加载内容</div>
</div>
</body>
</html>

原理很简单,通过注释可以很清楚是怎么实现的。

有人评论说

把偏移再加70将达到更好的滚动到底部再加载的效果。如下:(srollPos + dbHiht) >= (mainHiht-range+70)

最近在做自定义的cell的操作,由于之前是在网上找的一个类似的东东,结果拿来用的时候就出现了很多的问题,呵呵,结果我还是自己研究了一下,原因就是我之前是使用自定义的cell的问题是,在删除和非删除的过程中进行切换,有个选中和非选中的图片总是迟一步退去,导致的问题是,挡住了后面的内容,‍使得整个看起来不美观,结果我就是使用了UIImageView的隐藏功能,代码我放在下面,github也有的。在使用的时候最好是参照我的另一片博文:IPhone 自定义 UITableViewCell 行缩进(修正版)。好了,看代码。

attentionListCell.h

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
//
// attentionListCell.h
// xunYi6
//
// Created by david on 13-5-16.
// Copyright (c) 2013年 david. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface attentionListCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *imageViewPic;
@property (strong, nonatomic) IBOutlet UIImageView *selectImageView;


@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *index;
@property (strong, nonatomic) IBOutlet UILabel *rank;

@property (copy, nonatomic) UIImage *listImage;
@property (copy, nonatomic) NSString *listName;
@property (copy, nonatomic) NSString *listIndex;
@property (copy, nonatomic) NSString *listRank;
//--------------------------
//编辑操作
//--------------------------
@property (strong, nonatomic) UIImageView *checkImageView;
@property (nonatomic) BOOL checked;
- (void) setChecked:(BOOL)checked;

-(void) setSubViewsFrameEdit;
-(void) setSubViewsFrameNormal;

@end

attentionListCell.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// attentionListCell.m
// xunYi6
//
// Created by david on 13-5-16.
// Copyright (c) 2013年 david. All rights reserved.
//

#import "attentionListCell.h"

@implementation attentionListCell

@synthesize listImage = _listImage;
@synthesize listName = _listName;
@synthesize listIndex = _listIndex;
@synthesize listRank = _listRank;

@synthesize checkImageView = _checkImageView;

@synthesize selectImageView = _selectImageView;

@synthesize checked = _checked;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}

-(void) setListImage:(UIImage *)value
{
if(![value isEqual:_listImage])
{
_listImage = [value copy];
self.imageViewPic.image = _listImage;
}
}

-(void) setListName:(NSString *)value
{
if(![value isEqualToString:_listName])
{
_listName = [value copy];
self.name.text = _listName;
}
}

-(void) setListIndex:(NSString *)value
{
if(![value isEqualToString:_listIndex])
{
_listIndex = [value copy];
self.index.text = _listIndex;
}

}

-(void) setListRank:(NSString *)value
{
if(![value isEqualToString:_listRank])
{
_listRank = [value copy];
self.rank.text = _listRank;
}
}

-(void) willTransitionToState:(UITableViewCellStateMask)state{
[UIView beginAnimations:@"ResetFrame" context:nil];
[UIView setAnimationDuration:0.7];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self cache:NO];


if(state == UITableViewCellStateDefaultMask)
{
[self setSubViewsFrameNormal];
}
else if(state == UITableViewCellStateShowingEditControlMask)
{
[self setSubViewsFrameEdit];
}
else if(state == UITableViewCellStateShowingDeleteConfirmationMask)
{
[self setSubViewsFrameEdit];
}

[UIView commitAnimations];
}

-(void) setSubViewsFrameEdit{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

CGFloat offset = 10.0;

//
CGFloat selectImageX = -25.0;
CGFloat selectImageY = 19.5;
CGFloat selectImageWidth = 30.0;
CGFloat selectImageHeight = 30.0;

CGFloat imageX = 5.0 + offset;
CGFloat imageY = 5.0;
CGFloat imageWidth = 60.0;
CGFloat imageHeight = 60.0;

CGFloat nameX = 70.0 + offset;
CGFloat nameY = 5.0;
CGFloat nameWidth = 240.0 - offset;
CGFloat nameHeight = 15.0;

CGFloat indexX = 70.0 + offset;
CGFloat indexY = 25.0;
CGFloat indexWidth = 240.0 - offset;
CGFloat indexHeight = 15.0;

CGFloat rankX = 70.0 + offset;
CGFloat rankY = 45.0;
CGFloat rankWidth = 240.0 - offset;
CGFloat rankHeight = 15.0;

[self.imageViewPic setFrame:CGRectMake(imageX, imageY, imageWidth, imageHeight)];

[self.name setFrame:CGRectMake(nameX, nameY, nameWidth, nameHeight)];

[self.index setFrame:CGRectMake(indexX, indexY, indexWidth, indexHeight)];

[self.rank setFrame:CGRectMake(rankX, rankY, rankWidth, rankHeight)];

[self.selectImageView setFrame:CGRectMake(selectImageX, selectImageY, selectImageWidth, selectImageHeight)];
_selectImageView.hidden = NO;

[UIView commitAnimations];
}


-(void) setSubViewsFrameNormal{
if(_checkImageView != nil){
[_checkImageView setFrame:CGRectMake(0.0, 20.0, 0.0, 0.0)];
}

CGFloat offset = 0.0;
//
CGFloat selectImageX = 1.0;
CGFloat selectImageY = 19.5;
CGFloat selectImageWidth = 30.0;
CGFloat selectImageHeight = 30.0;

CGFloat imageX = 5.0 + offset;
CGFloat imageY = 5.0;
CGFloat imageWidth = 60.0;
CGFloat imageHeight = 60.0;

CGFloat nameX = 70.0 + offset;
CGFloat nameY = 30.0;
CGFloat nameWidth = 240.0 - offset;
CGFloat nameHeight = 15.0;

CGFloat indexX = 70.0 + offset;
CGFloat indexY = 25.0;
CGFloat indexWidth = 240.0 - offset;
CGFloat indexHeight = 15.0;

CGFloat rankX = 70.0 + offset;
CGFloat rankY = 45.0;
CGFloat rankWidth = 240.0 - offset;
CGFloat rankHeight = 15.0;

[self.imageViewPic setFrame:CGRectMake(imageX, imageY, imageWidth, imageHeight)];

[self.name setFrame:CGRectMake(nameX, nameY, nameWidth, nameHeight)];

[self.index setFrame:CGRectMake(indexX, indexY, indexWidth, indexHeight)];

[self.rank setFrame:CGRectMake(rankX, rankY, rankWidth, rankHeight)];

[self.selectImageView setFrame:CGRectMake(selectImageX, selectImageY, selectImageWidth, selectImageHeight)];
_selectImageView.hidden = YES;
}

//-------------------------------
// 重新定义editing的设置
//-------------------------------
-(void) setEditing:(BOOL)editing animated:(BOOL)animated{

if (self.editing == editing){
return;
}

[super setEditing:editing animated:animated];

if (editing){
CGFloat offset = 10.0;
//
CGFloat selectImageX = -25.0;
CGFloat selectImageY = 19.5;
CGFloat selectImageWidth = 30.0;
CGFloat selectImageHeight = 30.0;


CGFloat imageX = 5.0 + offset;
CGFloat imageY = 5.0;
CGFloat imageWidth = 60.0;
CGFloat imageHeight = 60.0;

CGFloat nameX = 70.0 + offset;
CGFloat nameY = 5.0;
CGFloat nameWidth = 240.0 - offset;
CGFloat nameHeight = 15.0;

CGFloat indexX = 70.0 + offset;
CGFloat indexY = 25.0;
CGFloat indexWidth = 240.0 - offset;
CGFloat indexHeight = 15.0;

CGFloat rankX = 70.0 + offset;
CGFloat rankY = 45.0;
CGFloat rankWidth = 240.0 - offset;
CGFloat rankHeight = 15.0;

[self.imageViewPic setFrame:CGRectMake(imageX, imageY, imageWidth, imageHeight)];

[self.name setFrame:CGRectMake(nameX, nameY, nameWidth, nameHeight)];

[self.index setFrame:CGRectMake(indexX, indexY, indexWidth, indexHeight)];

[self.rank setFrame:CGRectMake(rankX, rankY, rankWidth, rankHeight)];

[self.selectImageView setFrame:CGRectMake(selectImageX, selectImageY, selectImageWidth, selectImageHeight)];

_selectImageView.hidden = NO;

NSString *unselectedPath = [[NSBundle mainBundle] pathForResource:@"attention_unselect" ofType:@"png"];
_selectImageView.image = [UIImage imageWithContentsOfFile:unselectedPath];


[self setChecked:_checked];
}else{
_checked = NO;

}
}


- (void) setChecked:(BOOL)checked{
if (checked){
NSString *selectedPath = [[NSBundle mainBundle] pathForResource:@"attention_select" ofType:@"png"];
_selectImageView.image = [UIImage imageWithContentsOfFile:selectedPath];
self.backgroundView.backgroundColor = [UIColor whiteColor];
}else{
NSString *unselectedPath = [[NSBundle mainBundle] pathForResource:@"attention_unselect" ofType:@"png"];
_selectImageView.image = [UIImage imageWithContentsOfFile:unselectedPath];
self.backgroundView.backgroundColor = [UIColor whiteColor];
}
_checked = checked;
}

@end

代码就是这样的。

以前经常使用xib做自定义的UITableViewCell,但是最近因为老是闪退,于是想知道是不是因为xib的问题,因为之前意识到一个问题就是兼容的问题,这里涉及到一个AutoLayout的问题。于是想不用xib文件试试。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSString *personImagePath = [[NSBundle mainBundle] pathForResource:@"Icon_NMI" ofType:@"png"];
UIImageView *personView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:personImagePath]];
[personView setFrame:CGRectMake(imageViewX,
imageViewY,
imageViewWidth,
imageViewHeight)];
[cell.contentView addSubview:personView];

UILabel *personLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabelX,
personView.frame.origin.y, self.view.frame.size.width - personView.frame.size.width,
titleLabelHeight)];
personLabel.text = [dic valueForKey:@"title"];
personLabel.font = [UIFont systemFontOfSize:18.0];
[cell.contentView addSubview:personLabel];

很简单的就实现了,如果想增加其他的东西,也是很方面的,只要将自己的组建添加到cell.contentView就好了。

We changed the home folder to /data/home/USER.

When I ssh to our centos server. It shows error “Could not chdir to home directory /home/USER: Permission denied”, however loggin ok. I must manually run cd ~ to go to the home directory.

Googled around, and found it is caused by selinux. The solution:

To disabling selinux or change it from enforcing to permissive.
vi /etc/sysconfig/selinux
change SELINUX from enforcing to permissive: SELINUX=permissive, then reboot.

For server could not be rebooted:
#setenforce permissive

You can check if set correctly
#getenforce

上面就是解决的办法

其实就是修改一下/etc/sysconfig/selinux

将SELINUX修改为permissive

如果不想重启机器来使配置起作用,可以直接执行

1
setenforce permissive

如果没有实现,先确定一下是否是root权限

  • 使用updateCounters()来更新计数器字段。
1
Book::model()->updateCounters(['download_count' => 1], ':id=id', [':id' => $id]);
  • 使用sendFile()来下载文件。
1
2
3
4
5
6
7
$type = LFilter::checkString($_GET['t']);
$dataProvider = Book::model()->findByPk($id);
$content = $this->renderPartial('book', [
'dataProvider' => $dataProvider,
'type' => $type,
], true, false);
Yii::app()->request->sendFile($dataProvider->name . '.' . $type, $content),
  • 设计数据库时候如果create_time, update_time字段为int(10).在模型中使用行为插件。
1
2
3
4
5
6
7
8
9
10
public function behaviors()
{
return [
'CTimestampBehavior' => [
'class' => 'zii.behaviors.CTimestampBehavior',
'createAttribute' => 'create_time',
'updateAttribute' => 'update_time',
],
];
}
  • 如果有些字段使用1,2,3之类的数字存储,在程序中使用时候含义不明确。可以model中加入如下函数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private static $_items = [];
public function loadItems($type, $code = null)
{
self::$_items = [
'status' => [
'1' => Yii::t('dh', '开启'),
'2' => Yii::t('dh', '关闭'),
],
'type' => [
'1' => Yii::t('dh', '产品'),
'2' => Yii::t('dh', '文章'),
],
];
return $code ? self::$_items[$type][$code] : self::$_items[$type];
}
  • 多语言使用Yii::t()函数。
1
2
3
4
5
6
7
8
9
10
11
public function attributeLabels()
{
return [
'verifyCode' => Yii::t('default', '验证码'),
'name' => Yii::t('default', '名字'),
'email' => Yii::t('default', '邮箱'),
'subject' => Yii::t('default', '标题'),
'body' => Yii::t('default', '内容'),
'required' => Yii::t('default', '变量"{var}"没有定义'array('{var}' => $var));,
];
}
  • 数据库表使用了前缀,则使用{{$tableName}}
1
2
3
4
public function tableName()
{
return '{{product}}';
}
  • Cookie的使用
1
2
3
4
5
6
7
8
9
10
//设置Cookie
$cookie=new CHttpCookie($name,$value);
$cookie=time()+60*60*24;
Yii::app()->request->cookies[$name]=$cookie;
//获取Cookie
$cookie=Yii::app()->request->cookies[$name];
$value=$cookie->value;
//删除Cookie
$cookie = Yii::app()->request->getCookies();
unset($cookie[$name]);
  • zii.widgets.jui.CJuiTabs的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$this->widget('zii.widgets.jui.CJuiTabs', array(
'tabs'=>array(
Yii::t('book','基本信息') => $this->renderPartial('_basic',array(
'model' => $model,
'form' => $form,
),true),
Yii::t('book','作品标签') => array('ajax'=> $this->createUrl('tag/boxList',array('tag'=>$model->tag))),
Yii::t('book','作品封面') => $this->renderPartial('_image',array(
'model'=>$model,
'form'=>$form,
),true),
Yii::t('book','作品公告') => $this->renderPartial('_notice',array(
'model'=>$model,
'form'=>$form,
),true),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>false,
),
));
?>

对于tabs而言,对于复杂内容的渲染结合使用renderPartial();

  • zii.widgets.grid.CGridView的使用
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
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'chapter-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
//锚点<a href="http://blog.163.com/huv520@126/blog/"></a>
array(
'name'=>'name',
'type'=>'raw',
'value'=>'CHtml::link($data->name,"/book/$data->id")',
),
//图片
array(
'name'=>'image',
'type'=>'image',
'value'=>'LImages::getPath("book").$data->image',//图片相对路径
),
//下拉列表
array(
'name'=>'type',
'value'=>'Lookup::item("chapterType",$data->type)',
'filter'=>Lookup::items('chapterType'),
),
//内容截取
array(
'name'=>'content',
'type'=>'html',
'value'=>'mb_substr(htmlspecialchars_decode($data->content),0,100,"utf-8")',
),
//时间
array(
'name'=>'create_time',
'type'=>'datetime',
),
// 根据相关信息读数据库
array(
'name'=>'user_id',
'value'=>'User::model()->findbyPk($data->user_id)->username',
'filter'=>false,
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
  • findAll()的使用;
1
2
3
4
5
$params = [
'select' => 'id,name,image',
'order' => 'total_point DESC',
'limit' => 5,
];

对于我跟人来说,关于在一个控制器的中做action的post限制,一般的话我是这样做的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public function actionDownload()
{
if (Yii::app()->request->isPOSTRequest) {
$message = [];
if (isset($_POST['id']) && isset($_POST['url'])) {}
if ($this->fileClick($_POST['id'], $_POST['url'])) {
$message['code'] = 1;
} else {
$message['code'] = 0;
}
echo json_encode($message);
}
throw new CHttpException(400, '请求的页面不存在.');
}

感觉还不错,但是今天通过设置权限总是遇到错误,结果我发现了一个小角落的问题被我忽视了

1
2
3
4
5
6
7
public function filters()
{
return [
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
];
}

postOnly + delete就是这里

yii的控制还是不错的,继续感悟

关于这个说明,我引用了www.yiiframework.com 里面的qiang哥的

提到了两个问题:一是SQL Injection攻击,一个是XSS攻击。

对于前者,需要避免的是直接把用户输入嵌入到SQL里,例如:”SELECT * FROM tbl_user WHERE id={$_GET['id']}“。
恶意用户可以让$_GET['id']等于”1; DELETE FROM tbl_user“,这样就把所有的用户数据都删除了!非常危险!

解决办法有好几种。最简单的就是用param binding,请阅读PHP PDO获得相关知识。如果知道id是整数,也可以先把输入强制为整数。或者如果id是字串,可以用CDbConnection::quoteValue()把输入加上引号。如果你用的是AR,那么save()函数自动会使用param binding。如果你用findAll()之类的函数,自己生成condition部分,那就要特别小心不要直接嵌入输入。

XSS攻击主要是要避免直接显示用户的输入数据。例如echo $user->description(假设description的数据来自用户的输入)。恶意用户可以让id为一段js代码,使得其它用户查看该页面后隐式执行该代码,从而被恶意用户获得登录cookie等安全信息。

解决办法很简单,就是用CHtml::encode()。如果输入是HTML,可以用CHtmlPurifier::purify()来过滤有害代码。

0%