主页 Type-Safe Casting in Objective-C
Post
Cancel

Type-Safe Casting in Objective-C

Preface

This article carries strong personal feelings. If you feel uncomfortable reading it, please close it as soon as possible. This article is only for personal learning records. Reposting or sharing within the scope of the license is welcome. Please respect the copyright and keep the original link. Thank you for your understanding and cooperation. If you think this site can help you, you can subscribe to this site via RSS. Thanks for your support!

CAST

Cast is also called currying; it’s simply a type conversion process. In iOS we often force-cast some data type. Every time we have to write some bloated code, so people get used to writing macros to check whether an instance variable belongs to a certain class.

Below I’ll share some commonly used macros that leverage the dynamic features provided by the Objective-C runtime to handle common type checks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifdef __cplusplus
extern "C" {
#endif

id YZSafeCast(id obj, Class classType);

#ifdef __cplusplus
}
#endif


#ifndef YZ_SAFE_CAST

/// Safe type casting (currying)
#define YZ_SAFE_CAST(obj, asClass)  YZSafeCast(obj, [asClass class])

#endif

The implementation file

1
2
3
4
5
6
7
8
9
10
11
12
#import <Foundation/Foundation.h>
#import "YZSafeCast.h"

id YZSafeCast(id obj, Class classType)
{
    if ([obj isKindOfClass:classType])
    {
        return obj;
    }
    return classType ? nil : obj;
}

Usage is as follows

1

Summary

Recording some techniques that are commonly used but not immediately findable when you need them

demo

该博客文章由作者通过 CC BY 4.0 进行授权。