pEpObjCAdapter/NSArray+Extension.m
author Dirk Zimmermann <dz@pep.security>
Thu, 12 Sep 2019 16:41:52 +0200
branchIOSAD-141
changeset 1245 cd15ff3a40b9
parent 187 1f630a05e444
permissions -rw-r--r--
IOSAD-141 Don't share session between threads.
dirk@187
     1
//
dirk@187
     2
//  NSArray+Extension.m
dirk@187
     3
//  pEpiOSAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Dirk Zimmermann on 03.05.17.
dirk@187
     6
//  Copyright © 2017 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
#import "NSArray+Extension.h"
dirk@187
    10
dirk@187
    11
@implementation ArrayTake
dirk@187
    12
dirk@187
    13
- (instancetype)initWithElements:(NSArray * _Nonnull)elements rest:(NSArray * _Nonnull)rest
dirk@187
    14
{
dirk@187
    15
    if (self = [super init]) {
dirk@187
    16
        _elements = elements;
dirk@187
    17
        _rest = rest;
dirk@187
    18
    }
dirk@187
    19
    return self;
dirk@187
    20
}
dirk@187
    21
dirk@187
    22
@end
dirk@187
    23
dirk@187
    24
@implementation NSArray (Extension)
dirk@187
    25
dirk@187
    26
- (ArrayTake * _Nullable)takeOrNil:(NSInteger)count
dirk@187
    27
{
dirk@187
    28
    if (self.count >= count) {
dirk@187
    29
        NSInteger restCount = self.count - count;
dirk@187
    30
        NSArray *elements = [self subarrayWithRange:NSMakeRange(0, count)];
dirk@187
    31
        NSArray *rest = [self subarrayWithRange:NSMakeRange(count, restCount)];
dirk@187
    32
        ArrayTake *taken = [[ArrayTake alloc] initWithElements:elements rest:rest];
dirk@187
    33
        return taken;
dirk@187
    34
    }
dirk@187
    35
    return nil;
dirk@187
    36
}
dirk@187
    37
dirk@187
    38
@end