Objective-C: How to check if the key and value data types are expected data types in a NSDictionary?

  1. Scenario: “If you writing a unit test to check if the input parameters are expected data types.”
if ([obj isKindOfClass:[ExampleClass class]]) {
NSLog(@"The obj belongs to this class");
} else {
NSLog(@"The obj does not belong to this class");
}
NSEnumerator *keyEnumerator = [myDictionary keyEnumerator];
NSEnumerator *objectEnumerator = [myDictionary objectEnumerator];
id value;
id key;
BOOL valid = YES;

while ((key = [keyEnumerator nextObject])) {
if (![key isKindOfClass:[ExampleClass class]]) {
NSLog(@"The key does not belong to this class");
valid = NO;
break;
}
}
if (valid) {
while((value = [objectEnumerator nextObject])) {
if (![value isKindOfClass:[ExampleClass class]]) {
NSLog(@"The value does not belong to this class");
valid = NO;
break;
}
}
}
__block BOOL valid = YES;[myDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
if (![key isKindOfClass:[ExampleClass class]]) {
NSLog(@"The key does not belong to this class");
valid = NO;
*stop = YES;
}
if (![value isKindOfClass:[ExampleClass class]]) {
NSLog(@"The value does not belong to this class");
valid = NO;
*stop = YES;
}
}];

--

--

--

Software Engineer (Android, iOS, Unity) | M.Sc. Computer Science — Technische Universitaet Berlin | Mobile Advertising, Attribution, Monetisation, UA & Analytic

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flirting With Flutter: Is Cross-Platform App Development Mainstream Ready?

Journey into GSoC — Part 2

The Middle Way: Annotation-based Transaction Management

3 Tips Experienced Developers Use to Get Better Ideas

Announcement of the Adjustment of NerveNetwork Staking Parameters

0xApes v2

Compiling a C program(gcc main.c)

How to develop Dynamics 365 ALM Strategy — Part 4

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akarsh SEGGEMU

Akarsh SEGGEMU

Software Engineer (Android, iOS, Unity) | M.Sc. Computer Science — Technische Universitaet Berlin | Mobile Advertising, Attribution, Monetisation, UA & Analytic

More from Medium

Clustered vs Nonclustered Index Cheatsheet

What to look for in a Code Review

LeetCode 1211 Queries Quality and percentage

Improve Scalability Using Factory Method Design Pattern