欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Objective-c 的一些学习

程序员文章站 2022-03-09 20:10:56
...
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];
}
相关标签: Cocoa Objective-C