收集些不懂的iphone代码

#import "NSString+URLEscaped.h"

@implementation NSString (URLEscaped)
- (NSString *)URLEscaped {
    CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)@"&+/?=", kCFStringEncodingUTF8);
    NSString *out = [NSString stringWithString:(NSString *)escaped];
    CFRelease(escaped);
    return [[out copy] autorelease];
}
- (NSString *)unURLEscape {
    CFStringRef unescaped = CFURLCreateStringByReplacingPercentEscapes(NULL, (CFStringRef)self, (CFStringRef)@"");
    NSString *out = [NSString stringWithString:(NSString *)unescaped];
    CFRelease(unescaped);
    return [[out copy] autorelease];
}

@end

============================================================

#import "NSData+Compress.h"
#include <zlib.h>

@implementation NSData (Compress)
-(NSData *)compressWithLevel:(NSInteger)compressionLevel {
    NSMutableData *bazip;
    if ([self length] == 0) {
        return nil;
    }
    if (compressionLevel < -1 || compressionLevel > 9)
        compressionLevel = -1;
   
    unsigned long len = [self length] + [self length] / 100 + 13;
    int res;
   
    do {
        bazip = [NSMutableData data];
        [bazip setLength:len+4];
        res = compress2([bazip mutableBytes]+4, &len, [self bytes], [self length], compressionLevel);
       
        switch (res) {
            case Z_OK:
                ((char *)[bazip mutableBytes])[0] = ([self length] & 0xff000000) >> 24;
                ((char *)[bazip mutableBytes])[1] = ([self length] & 0x00ff0000) >> 16;
                ((char *)[bazip mutableBytes])[2] = ([self length] & 0x0000ff00) >> 8;
                ((char *)[bazip mutableBytes])[3] = ([self length] & 0x000000ff);
                break;
            case Z_MEM_ERROR:
                NSLog(@"Compress: Z_MEM_ERROR: Not enough memory");
                [bazip setLength:0];
                break;
            case Z_BUF_ERROR:
                len *= 2;
                break;
        }
    } while (res == Z_BUF_ERROR);
   
    return [NSData dataWithData:bazip];
}
@end
==================================================

你可能感兴趣的:(收集些不懂的iphone代码)