74_iOS干货40_结构体和对象的相互转化 + block的写法 + 自定义打印

一,结构体和对象的相互转化 

1,系统结构体


 //结构体转NSValue的方法
+ (NSValue *)valueWithCGPoint:(CGPoint)point;
+ (NSValue *)valueWithCGVector:(CGVector)vector;
+ (NSValue *)valueWithCGSize:(CGSize)size;
+ (NSValue *)valueWithCGRect:(CGRect)rect;
+ (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;
+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;
+ (NSValue *)valueWithUIOffset:(UIOffset)insets NS_AVAILABLE_IOS(5_0);
 
//NSValue转结构体的方法
- (CGPoint)CGPointValue;
- (CGVector)CGVectorValue;
- (CGSize)CGSizeValue;
- (CGRect)CGRectValue;
- (CGAffineTransform)CGAffineTransformValue;
- (UIEdgeInsets)UIEdgeInsetsValue;
- (UIOffset)UIOffsetValue NS_AVAILABLE_IOS(5_0);
 



//结构体转NSValue的使用
    CGPoint point = CGPointMake(0, 0);
    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];
    NSValue *value = [NSValue valueWithCGPoint:point];
    [array addObject:value];

//NSValue转结构体的使用
    NSValue *tmpValue = array[0];
    CGPoint tmpPoint = [tmpValue CGPointValue];

2,自定义结构体

//   自定义的结构体
    struct Test {
        int ID;
        CGFloat  height;
    };
    typedef struct Test Test;
    
//    自定义结构体封箱操作
    Test tmpTest;
    tmpTest.height = 80.0;
    tmpTest.ID = 100;
    NSLog(@"id==%d,height==%f",tmpTest.ID,tmpTest.height);
    
    NSValue *customValue = [NSValue valueWithBytes:&tmpTest objCType:@encode(struct Test)];

//    拆箱操作
    Test tmpTest1;
    [customValue getValue:&tmpTest1];
    NSLog(@"id==%d,height==%f",tmpTest1.ID,tmpTest1.height);

二,block的写法

  1. block作为属性
    1. //block属性的定义
      @property(nonatomic,strong)void(^block)();
      
      //block属性的使用
      
      Dog *dog = [[Dog alloc]init];
      
      void(^block_MBXB)() = ^(){
      
      NSLog(@"狗狗不可以随便拉粑粑");
      
      };
      
      dog.block = block_MBXB;
      
      dog.block();

       

  2. block作为参数
    1. //block作为方法参数的定义和实现
      - (void)eat:(void(^)(NSString*))block;
      - (void)eat:(void(^)(NSString*))block{
      
      block(@"狗粮");
      
      }
      
      
      //block作为方法参数的使用
      Dog *dog = [[Dog alloc]init];
      
      [dog eat:^(NSString *food) {
      
      NSLog(@"狗狗吃%@,吃了一大盆",food);
      
      }];

       

  3. block作为返回值
    1. //block作为返回值的定义和实现
      - (void(^)(int))eatFood;
      - (void(^)(int))eatFood{
      
      return ^(int food){
      
      NSLog(@"狗狗吃了狗粮%d斤",food);
      
      };
      
      }
      
      //block作为返回值的使用
      Dog *dog = [[Dog alloc]init];
      dog.eatFood(200);

       

三,自定义打印

/*打印相关(
分三个部分
1,FILE:当前文件名 + LINE:类的第几行 + func:函数名称 
     FILE 宏在预编译时会替换成当前的源文件名
     LINE宏在预编译时会替换成当前的行号
     FUNCTION宏在预编译时会替换成当前的函数名称

2,打印的内容 :##__VA_ARGS__
     VA_ARGS 是一个可变参数的宏,很少人知道这个宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持)。宏前面加上##的作用在于,当可变参数的个数为0时,这里的##起到把前面多余的”,”去掉的作用,否则会编译出错

3,换行符
*/

#define MCLog(format, ...) do {                                                                          \
fprintf(stderr, "<%s : %d> %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],__LINE__, __func__);                                                        \
(NSLog)((format), ##__VA_ARGS__);                                           \
fprintf(stderr, "✅✅✅✅✅\n");                                               \
} while (0)

打印的结果示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_map是C++标准库中的容器,用于存储键-值对并提供快速的查找功能。对于自定义结构体,可以使用unordered_map来存储该结构体作为键,并指定自定义的哈希函数和键的比较规则。 有三种常见的方式可以实现这个目标: 方法一:将自定义结构体中重载operator == 和自定义hash函数,并在unordered_map中传入自定义结构体类型和自定义hash函数类型,不传入第四个参数。 ```cpp #include <iostream> #include <unordered_map> #include <functional> class Person { public: int _age; Person(int age = -1) :_age(age) {} bool operator == (const Person& p) const { return _age == p._age; } }; struct PersonHash { public: size_t operator()(const Person& p) const { return std::hash<int>()(p._age); } }; int main() { std::unordered_map<Person, int, PersonHash> um; um.insert(std::make_pair<Person, int>(Person(1), 1)); um.insert(std::make_pair<Person, int>(Person(2), 1)); for (auto& e : um) { std::cout << e.first._age << " " << e.second << std::endl; } return 0; } ``` 方法二:自定义hash函数和自定义键的比较规则,并在unordered_map中传入自定义结构体类型、自定义hash函数类型和自定义键的比较规则类型。 ```cpp #include <iostream> #include <unordered_map> #include <functional> class Person { public: int _age; Person(int age = -1) :_age(age) {} }; struct PersonHash { public: size_t operator()(const Person& p) const { return std::hash<int>()(p._age); } }; struct PersonEqual { public: bool operator()(const Person& p1, const Person& p2) const { return p1._age == p2._age; } }; int main() { std::unordered_map<Person, int, PersonHash, PersonEqual> um; um.insert(std::make_pair<Person, int>(Person(1), 1)); um.insert(std::make_pair<Person, int>(Person(2), 1)); for (auto& e : um) { std::cout << e.first._age << " " << e.second << std::endl; } return 0; } ``` 方法三:传入自定义结构体类型,不重载operator == 和自定义hash函数。 ```cpp #include <iostream> #include <unordered_map> class Person { public: int _age; Person(int age = -1) :_age(age) {} }; int main() { std::unordered_map<Person, int> um; um.insert(std::make_pair<Person, int>(Person(1), 1)); um.insert(std::make_pair<Person, int>(Person(2), 1)); for (auto& e : um) { std::cout << e.first._age << " " << e.second << std::endl; } return 0; } ``` 以上是三种使用unordered_map存储自定义结构体的方法,具体使用哪种方式取决于你的需求和喜好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值