zoukankan      html  css  js  c++  java
  • iOS

    前言

    	@interface NSValue : NSObject <NSCopying, NSSecureCoding>
    
    • 将任意数据类型包装成 OC 对象

    1、比较两个 NSValue 类型数据的大小

    	NSValue *value1 = [NSValue valueWithPoint:NSMakePoint(10, 32)];
    	NSValue *value2 = [NSValue valueWithPoint:NSMakePoint(10, 32)];
    	    
    	// isEqualToValue
    	BOOL bl = [value1 isEqualToValue:value2];
    	    
    	if (bl) {
    		NSLog(@"value1 == value2");
    	} else {
    		NSLog(@"value1 != value2");
    	}
    

    2、NSValue 与 OC 数据类型的相互转换

    	// 将 OC 数据类型转换成 NSValue
    	NSValue *value1 = [NSValue valueWithPoint:NSMakePoint(10, 32)];
    	  	
    	// 将 NSValue 转换成 OC 数据类型
    	NSPoint point = [value1 pointValue];
    	NSLog(@"x: %.0f, y: %.0f", point.x, point.y);
    

    3、NSValue 与 自定义数据类型 的相互转换

    	// 将 自定义数据类型 转换成 NSValue
    	    
    		// 自定义结构体
    		typedef struct { int year; int month; int day; } MyDate;
    		
    		MyDate date = {2016, 1, 11};
    		    
    		// 取要转换的数据的地址
    		void *valueAdd = &date;
    		
    		// 将自定义数据类型装换成 C 语言字符串
    		char *objCType = @encode(MyDate);
    		
    		// 对象方法,将自定义数据类型转换成 NSValue
    		NSValue *value2 = [[NSValue alloc] initWithBytes:valueAdd objCType:objCType];
    		
    		// 类方法,将自定义数据类型转换成 NSValue
    		NSValue *value3 = [NSValue valueWithBytes:&date objCType:@encode(MyDate)];
    			
    	// 将 NSValue 转换成 自定义数据类型
    	
    		MyDate date1;
    		MyDate date2;
    		    
    		// 从 NSValue 中取出的值放到 &date1 中
    		[value2 getValue:&date1];
    		
    		// 从 NSValue 中取出的值放到 &date2 中
    		[value3 getValue:&date2];
    		    
    		NSLog(@"%i, %i, %i", date1.year, date1.month, date1.day);
    		NSLog(@"%i, %i, %i", date2.year, date2.month, date2.day);
    		    
    		// 从 NSValue 中取出自定义的数据类型
    		const char *type = [value2 objCType];
    		
    		NSLog(@"%@", [NSString stringWithUTF8String:type]);
    
  • 相关阅读:
    ora04063:view view_test has errors
    toad for oracle中登入界面Connect using框和TNSNames Editor框的显示状态
    格式化时间,转为中文
    一些简单有用的方法合集
    C#加密解密
    计算员工有效工作时间(C#版)
    计算员工有效工作时间(sql版)
    JavaScript 语言基础知识点总结(思维导图)
    正则表达式
    C#中请不要混淆引用类型和ref引用传参
  • 原文地址:https://www.cnblogs.com/QianChia/p/5782739.html
Copyright © 2011-2022 走看看