Apple开发_字符串与常用目标互换

1、OC 字符串与 C 字符串的相互转换

// OC 字符串转 C 字符串
const char *CString = [@"bei jing ni hao" UTF8String];

// C 字符串转 OC 字符串
NSString *OCString1 = [[NSString alloc] initWithCString:"ni hao" encoding:NSUTF8StringEncoding];
NSString *OCString2 = [[NSString alloc] initWithUTF8String:"ni hao"];

NSString *OCString3 = [NSString stringWithCString:"ni hao" encoding:NSUTF8StringEncoding];
NSString *OCString4 = [NSString stringWithUTF8String:"ni hao"];

2、NSString 与 NSData 的相互转换

NSString *string = @"bei jing nin hao";

// NSString 转 NSData
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

// data 类型数据以十六进制的形式打印出
NSLog(@"%@", data);

// data 类型数据以字符串的形式打印出
NSLog(@"%s", data.bytes);

// NSData 转 NSString
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

3、NSString 与 NSNumber 的相互转换

NSNumber *num1 = @10.456;

// NSNumber 转换 NSString
NSString *str = [num1 stringValue];

// NSString 转 NSNumber
NSNumber *num2 = [NSNumber numberWithFloat:[str floatValue]];

4、NSString 与 NSDate 的相互转换

NSDateFormatter *dfm = [[NSDateFormatter alloc] init];
dfm.dateFormat = @"yyyy年MM月dd日 HH点mm分ss秒";
dfm.locale = [NSLocale localeWithLocaleIdentifier:@"GMT+8"];

// NSString 转 NSDate
NSDate *date = [dfm dateFromString:@"2016年05月24日 17点38分03秒"];

// NSDate 转 NSString
NSString *str = [dfm stringFromDate:date];

5、NSString 与 Class 的相互转换

// NSString 转 Class
Class ClassName = NSClassFromString(@"Student");

Student *stu = [[ClassName alloc] init];
[stu studentTest];

// Class 转 NSString
NSString *strintFromClass = NSStringFromClass([Student class]);

6、NSString 与 方法名 的相互转换

// NSString 转 方法名
SEL selName = NSSelectorFromString(@"studentTest");

// 消除 performSelector: 方法警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

Student *stu = [[Student alloc] init];
[stu performSelector:selName];

#pragma clang diagnostic pop

// 方法名 转 NSString
NSString *stringFromSel = NSStringFromSelector(@selector(studentTest));

7、NSString 与 NSProtocol 的相互转换

// NSString 转 NSProtocol
Protocol *ptlName = NSProtocolFromString(@"NSCopying");

Student *stu = [[Student alloc] init];
BOOL conforms = [stu conformsToProtocol:ptlName];

// NSProtocol 转 NSString
NSString *stringFromProtocol = NSStringFromProtocol(@protocol(NSCopying));

8、NSString 与 NSRange 的相互转换

NSRange range4 = NSMakeRange(9, 4);

// NSRange 转 NSString
NSString *stringFronRange = NSStringFromRange(range4);

// NSString 转 NSRange
NSRange range5 = NSRangeFromString(stringFronRange);

9、NSString 与 NSPoint 的相互转换

NSPoint point5 = NSMakePoint(9, 4);

// NSPoint 转 NSString
NSString *stringFronPoint = NSStringFromPoint(point5);

// NSString 转 NSPoint
NSPoint point6 = NSPointFromString(stringFronPoint);

10、NSString 与 NSSize 的相互转换

NSSize size5 = NSMakeSize(9, 4);

// NSSize 转 NSString
NSString *stringFronSize = NSStringFromSize(size5);

// NSString 转 NSSize
NSSize size6 = NSSizeFromString(stringFronSize);

11、NSString 与 NSRect 的相互转换

NSRect rect5 = NSMakeRect(9, 4, 4, 9);

// NSRect 转 NSString
NSString *stringFronRect = NSStringFromRect(rect5);

// NSString 转 NSRect
NSRect rect6 = NSRectFromString(stringFronRect);

12、生成随机数

// 生成 15 位长度的数字字母组合的随机数
- (NSString *)generateTradeNO {

	static int kNumber = 15;

	NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	NSMutableString *resultStr = [[NSMutableString alloc] init];
	srand((unsigned)time(0));
	for (int i = 0; i < kNumber; i++) {
		unsigned index = rand() % [sourceStr length];
		NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
		[resultStr appendString:oneStr];
	}

	return resultStr;
}
posted @ 2019-02-16 22:42  CH520  阅读(193)  评论(0编辑  收藏  举报