From 6d169ea203e146a20facbe5863eb0c8f7fdd1b17 Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Tue, 2 Jan 2024 17:10:46 -0500 Subject: [PATCH] add collection safe subscript for index out of bound safety --- MVMCore/MVMCore.xcodeproj/project.pbxproj | 4 ++++ MVMCore/MVMCore/Categories/Collection+Safe.swift | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 MVMCore/MVMCore/Categories/Collection+Safe.swift diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index dbf0469..da71c4e 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ 1DAD0FFE26AAB40000216E83 /* ActionRunJavaScriptModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DAD0FFD26AAB3FF00216E83 /* ActionRunJavaScriptModel.swift */; }; 2723337B28BD534D004EAEE0 /* MVMCoreEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2723337A28BD534D004EAEE0 /* MVMCoreEvent.swift */; }; 2723337D28BD53C2004EAEE0 /* Date+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2723337C28BD53C2004EAEE0 /* Date+Extension.swift */; }; + 5846ABF42B44BB9000FA6C76 /* Collection+Safe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5846ABF32B44BB9000FA6C76 /* Collection+Safe.swift */; }; 6042E8FC2B317B190031644B /* MVMCoreLoggingHandlerHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 6042E8FB2B3094680031644B /* MVMCoreLoggingHandlerHelper.h */; settings = {ATTRIBUTES = (Public, ); }; }; 605A9A2A2ABD712F00487E47 /* MVMCoreLoggingHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 605A9A292ABD712F00487E47 /* MVMCoreLoggingHandler.swift */; }; 6079EDCE2AD97AA5004B7A85 /* MVMCoreLoggingDelegateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6079EDCD2AD97AA5004B7A85 /* MVMCoreLoggingDelegateProtocol.swift */; }; @@ -191,6 +192,7 @@ 2723337C28BD53C2004EAEE0 /* Date+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extension.swift"; sourceTree = ""; }; 581FABEE2A71D0E6003A8508 /* mvmcore_dev.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = mvmcore_dev.xcconfig; sourceTree = ""; }; 5836B8E22A4338DF002553D9 /* mvmcore.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = mvmcore.xcconfig; sourceTree = ""; }; + 5846ABF32B44BB9000FA6C76 /* Collection+Safe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Collection+Safe.swift"; sourceTree = ""; }; 6042E8FB2B3094680031644B /* MVMCoreLoggingHandlerHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreLoggingHandlerHelper.h; sourceTree = ""; }; 605A9A292ABD712F00487E47 /* MVMCoreLoggingHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVMCoreLoggingHandler.swift; sourceTree = ""; }; 6079EDCD2AD97AA5004B7A85 /* MVMCoreLoggingDelegateProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVMCoreLoggingDelegateProtocol.swift; sourceTree = ""; }; @@ -408,6 +410,7 @@ 8876D5D91FB50AB000EB2E3D /* Categories */ = { isa = PBXGroup; children = ( + 5846ABF32B44BB9000FA6C76 /* Collection+Safe.swift */, 01DF561321F90ADC00CC099B /* Dictionary+MFConvenience.swift */, D282AAB72240342D00C46919 /* NSNumber+Extension.swift */, 8876D5DA1FB50AB000EB2E3D /* NSArray+MFConvenience.h */, @@ -900,6 +903,7 @@ AF43A5881FBB67D6008E9347 /* MVMCoreActionUtility.m in Sources */, AFED77A61FCCA29400BAE689 /* MVMCoreViewControllerStoryBoardMappingObject.m in Sources */, 016CF36925FA6DD400B82A1F /* ClientParameterHandler.swift in Sources */, + 5846ABF42B44BB9000FA6C76 /* Collection+Safe.swift in Sources */, AF69D4F7286EA0B800BC6862 /* ActionPreviousSubmitHandler.swift in Sources */, AF43A57C1FBA5E6A008E9347 /* MVMCoreHardcodedStringsConstants.m in Sources */, 0AFF597A23FC6E60005C24E8 /* ActionShareModel.swift in Sources */, diff --git a/MVMCore/MVMCore/Categories/Collection+Safe.swift b/MVMCore/MVMCore/Categories/Collection+Safe.swift new file mode 100644 index 0000000..0943a9d --- /dev/null +++ b/MVMCore/MVMCore/Categories/Collection+Safe.swift @@ -0,0 +1,16 @@ +// +// Collection+Safe.swift +// MVMCore +// +// Created by Kyle on 2/13/20. +// Copyright © 2020 Verizon Wireless. All rights reserved. +// + +import Foundation + +public extension Collection { + /// Returns the element at the specified index if it is within bounds, otherwise nil. + subscript (safe index: Index) -> Element? { + return indices.contains(index) ? self[index] : nil + } +}