Latest 1.0.0
License MIT
Platforms ios 8.0

KAAOptional: Optionals for Objective C

Build status
Code coverage status

KAAOptional is Objective-C library which adds Optional support.


  • [x] Lightweight Generics for content value
  • [x] Dictionary now can return Optionals on subscript methods call
  • [x] Full Java 8 syntax (streams, control flow, functional extensions: map, flatMap, filter)
  • [x] Optional behave same way as swift optional: chaining support, doesn’t crash on unrecognised message sent if empty and crashes if not and value cannot respond, proxies messages to it’s value, behaves same on equality.
  • [x] Dot notation support
  • [x] Tested and well documented


Control flow:

Now in swift 5.0:

func fetch() -> String? {
//Some big work
return "123"

if let e = fetch() {
} else {
//Do something

> In Objective-C with KAAOptional:
  • (nonnull Optional<NSString > )fetch {
    //Some big work

    return Optional.of(@"123");

[[self fetch] ifPresent:^(NSNumber * _Nonnull aValue) {
NSLog(@"%@", aValue);
} orElse:^{

# Get functional extensions.

## Now in swift 5.0:

var res = "404"

if let e = fetch(),
let r = Int(e),
r > 123 {
res = e

## In Objective-C with KAAOptional using functional extensions:
  • (nonnull Optional<NSString > )fetch {
    //Some big work

    return Optional.of(@"123");

__auto_type const a = [[[self fetchResult] filter:^BOOL(NSString * _Nonnull aValue) {
return aValue.integerValue > 123 && [aValue isKindOfClass:[NSString class]];
}] orElse:@"404"];

NSLog(@"%@", a);

> or with .dot notation

__auto_type const b = [self fetchResult]
.filter(^BOOL(NSString * _Nonnull aValue) {
return aValue.integerValue > 123 && [aValue isKindOfClass:[NSString class]];
NSLog(@"%@", b);

Supports Java 8 style
- stream
- map
- flatMap
- filter

# Subscript on Dictionary now returns Optional.

## In swift:

var a = ["K": "V"]["K"] // Returns Optional
if let a = a { // Unwraps


## In Objective-C using Optional

You can enable it pointwise by calling *"op"* method on Dictionary.
Or enable it by default by calling *"[NSDictionary kaa_enableOptionalByDefault]"* once anywhere.
**⚠️⚠️⚠️ WARNING ⚠️⚠️⚠️**
Beware - even though Optional behaves like proxy: `-isEqual`, `-isKindOfClass`, `-isMemberOfClass` will work fine on old code,
calling `+class` and `+superClass` - will return `Optional` and `NSObject`.

__auto_type a = @{
@"K": @"V"
NSLog(@"%@", a[@"K"]); //-> <Optional: 0xSomeAddress, Value: @"V">
NSLog(@"%@", a[@"wrongKey"]); //-> <Optional: 0xSomeAddress, Value: nil>

> *Note: Following construct will not crash due to internal catch logic. So your old code is safe!*

[NSDictionary kaa_enableOptionalByDefault];

__auto_type a = [@{
@"K": [MyObject new]
}[@"K2"] myOldMessage];
// Will react same as nil messaging
NSLog(@"I didn’t crash!");

> *Note: Following construct will proxy to value. So it's also safe!*

[NSDictionary kaa_enableOptionalByDefault];

NSUInteger i = [@{@"K": @"V"} length];
NSLog(@"%@", @(i)); // Will output = 1

> *You can even chain subscript!!!*

__auto_type a = @{
@"K": @[@{@"K2": @"V"}]
//By using subscript, you get optional! An it can be chained!
__unused NSString *b = a[@"K"][0][@"K2"].orElse(@"V2");

# Equality

## In swift

var a = ["K": "V"]["K"]
print(a == "V") // true

## In Objective-C using KAAOptional

[@{@"K": @"V"}.op[@"K"] isEqual:@"K"] // YES

## Installation

### CocoaPods

target '<Your Target Name>' do
    pod 'KAAOptional', '~> 1.0.0'


github "neisip/KAAOptional"


KAAOptional is released under the MIT license. See LICENSE for details.

Latest podspec

    "name": "KAAOptional",
    "version": "1.0.0",
    "summary": "Optionals for Objective-C",
    "description": "KAAOptional provides Optionals with following features:n1) Genericsn2) Dot notationsn3) Chaining!n4) Dictionary subscription now returns Optionals!n5) Optionals don't crash on message which they don't know. (Same way as nil)n6) Optionals proxy messages to it's content!n7) Interoperable with swift!n8) Java 8 syntax",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Alexander Kazartsev": "[email protected]"
    "platforms": {
        "ios": "8.0"
    "source": {
        "git": "",
        "tag": "1.0.0"
    "source_files": [
    "exclude_files": "Classes/Exclude"

Pin It on Pinterest

Share This