Objective-c 的一些学习

Dictionary
NSDictionary * dictionary;  
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"good",@"why",@" bye",@"how",nil];  
NSMutableDictionary* dic = [NSMutableDictionary dictionary];
    if (spuId) {
        [dic setObject:spuId forKey:@"spuId"];
    }
    if (selectDoctorId) {
        [dic setObject:selectDoctorId forKey:@"selectDoctorId"];
    }
    if (isFirstTreatment ==0 || isFirstTreatment ==1 ) {
        [dic setObject:isFirstTreatment? @"true" : @"false" forKey:@"isFirstTreatment"];
    }
    if (isExpert ==0 || isExpert ==1 ) {
        [dic setObject:isExpert? @"true" : @"false" forKey:@"isExpert"];
    }
    if (gps) {
        [dic setObject:gps forKey:@"gps"];
NSEnumerator * enumerator =  [dictionary keyEnumerator];
    id obj;
    while( obj = [enumerator nextObject]) {
        NSString* val = [dictionary objectForKey:obj];
        NSString* param =(NSString*)obj;
        if (val) {
            if(![url hasSuffix:@"?"]){
                [url appendFormat:@"&"];
            }
            [url appendFormat:[NSString stringWithFormat:@"%@=%@", param, val]];
        }
    }

NSArray
NSMutableArray *docList = [NSMutableArray array];
        [docList removeAllObjects];
        [docList addObjectsFromArray:obj];


元编程

@interface NSString (URLFormat)

- (NSString *)stringByURLFormat:(NSDictionary *)dictionary;

@end

#import "NSString+URLFormat.h"

@implementation NSString (URLFormat)

- (NSString *)stringByURLFormat:(NSMutableDictionary *)dictionary{
    if([dictionary count]==0)
        return self;
    NSMutableString* url =  [[NSMutableString alloc] initWithString:self];
    [url appendFormat:@"?"];
    NSEnumerator * enumerator =  [dictionary keyEnumerator];
    id obj;
    while( obj = [enumerator nextObject]) {
        NSString* val = [dictionary objectForKey:obj];
        NSString* param =(NSString*)obj;
        if (val) {
            if(![url hasSuffix:@"?"]){
                [url appendFormat:@"&"];
            }
            [url appendFormat:[NSString stringWithFormat:@"%@=%@", param, val]];
        }
    }
    if([url hasSuffix:@"?"])
       [url deleteCharactersInRange:NSMakeRange (url.length-1, 1)];
    
    return url;
}

@end


1、字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];

2、字符串转int
int intString = [newString intValue];

3、int转字符串
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];

4、字符串转float
float floatString = [ newString floatValue];

5、float转字符串
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];

设置颜色
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert alpha:(CGFloat)alpha
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([cString length] < 6)
        return DEFAULT_VOID_COLOR;
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return DEFAULT_VOID_COLOR;
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:alpha];
}

猜你喜欢

转载自zhaoyifei.iteye.com/blog/2292274
今日推荐