Mobile Development 18 min read

JDTuple: Implementing a Tuple Data Structure in Objective‑C for Simplified Parameter Handling

This article introduces JDTuple, an Objective‑C tuple library that uses variadic functions, type encodings, and macro tricks to pack arbitrary values, provide key‑based and sequential unpacking, and simplify multi‑parameter passing and return values in iOS development.

JD Retail Technology
JD Retail Technology
JD Retail Technology
JDTuple: Implementing a Tuple Data Structure in Objective‑C for Simplified Parameter Handling

The article begins by highlighting the cumbersome nature of arrays and dictionaries in Objective‑C and proposes a tuple solution that can encapsulate any data without worrying about type or bounds, making OC code feel more like Swift.

It explains the concept of a tuple—originally a relational‑database row—and shows how a tuple can be represented as tuple1 = ("阿强", 18, "战士") , containing ordered or keyed elements that are immutable after definition.

Basic usage of JDTuple is demonstrated with the jd_tuple(...) constructor, e.g.: NSString *name = @"阿珍"; NSInteger age = 28; NSArray *arr = @[@"a", @"b", @"c"]; JDTuple *someTuple = jd_tuple(name, age, arr); The library supports anonymous tuples, sequential unpacking via jd_unpack(tuple)^(...){...} , and key‑based unpacking with jd_unpackWithkey(...) , allowing developers to extract only needed elements or ignore others using the arg_ph placeholder.

Implementation details cover how JDTuple stores values using an NSMutableArray of NSValue objects and a NSMutableDictionary mapping keys to indices. It leverages variadic functions and va_start / va_arg to read arguments, using a custom end marker ( jd_tuple_end ) instead of nil . Type encodings are obtained automatically with @encode(typeof(var)) , eliminating manual type specification.

Macros simplify tuple creation: #define jd_tuple(...) ({JDTuple *t = [JDTuple new]; [t tupleInputKeys:#__VA_ARGS__]; t.addArg(___tuple_add(__VA_ARGS__)); t;}) . Additional macros generate key lists, count arguments, and support strict unpacking ( jd_unpack_strict ) that enforces exact type matches at compile time.

Data retrieval can be performed by index or key using subscript syntax, e.g. NSValue *value = someTuple[0]; or NSValue *value = someTuple[@"age"]; . Batch key‑based unpacking is achieved through overloaded functions and helper macros that map keys to variables.

The article also showcases practical applications such as returning multiple values from a method, creating a universal performSelector_tuple: that accepts a tuple of arguments and returns a tuple of results, and converting JSON strings into tuples for lightweight data parsing.

Finally, it notes some pitfalls (e.g., BOOL vs. _Bool, architecture‑dependent sizes of CGFloat and NSInteger) and provides references to type‑encoding documentation, ReactCocoa, and macro techniques, with the source code hosted on Gitee.

iOSDataStructureObjective-CMacrotupleVariadic
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.