/* NSDictionary.h Copyright (c) 1994-2019, Apple Inc. All rights reserved. */ #import #import @class NSArray, NSSet, NSString, NSURL; /**************** Immutable Dictionary ****************/ NS_HEADER_AUDIT_BEGIN(nullability, sendability) @interface NSDictionary<__covariant KeyType, __covariant ObjectType> : NSObject @property (readonly) NSUInteger count; - (nullable ObjectType)objectForKey:(KeyType)aKey; - (NSEnumerator *)keyEnumerator; - (instancetype)init NS_DESIGNATED_INITIALIZER; - (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects forKeys:(const KeyType _Nonnull [_Nullable])keys count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; @end @interface NSDictionary (NSExtendedDictionary) @property (readonly, copy) NSArray *allKeys; - (NSArray *)allKeysForObject:(ObjectType)anObject; @property (readonly, copy) NSArray *allValues; @property (readonly, copy) NSString *description; @property (readonly, copy) NSString *descriptionInStringsFileFormat; - (NSString *)descriptionWithLocale:(nullable id)locale; - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level; - (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary; - (NSEnumerator *)objectEnumerator; - (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(ObjectType)marker; /* Serializes this instance to the specified URL in the NSPropertyList format (using NSPropertyListXMLFormat_v1_0). For other formats use NSPropertyListSerialization directly. */ - (BOOL)writeToURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)); - (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator; // count refers to the number of elements in the dictionary - (void)getObjects:(ObjectType _Nonnull __unsafe_unretained [_Nullable])objects andKeys:(KeyType _Nonnull __unsafe_unretained [_Nullable])keys count:(NSUInteger)count API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) NS_SWIFT_UNAVAILABLE("Use 'allKeys' and/or 'allValues' instead"); - (nullable ObjectType)objectForKeyedSubscript:(KeyType)key API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); - (void)enumerateKeysAndObjectsUsingBlock:(void (NS_NOESCAPE ^)(KeyType key, ObjectType obj, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(KeyType key, ObjectType obj, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (NSArray *)keysSortedByValueUsingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (NSSet *)keysOfEntriesPassingTest:(BOOL (NS_NOESCAPE ^)(KeyType key, ObjectType obj, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(KeyType key, ObjectType obj, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); @end @interface NSDictionary (NSDeprecated) /// This method is unsafe because it could potentially cause buffer overruns. You should use -getObjects:andKeys:count: - (void)getObjects:(ObjectType _Nonnull __unsafe_unretained [_Nullable])objects andKeys:(KeyType _Nonnull __unsafe_unretained [_Nullable])keys NS_SWIFT_UNAVAILABLE("Use 'allKeys' and/or 'allValues' instead") API_DEPRECATED("Use -getObjects:andKeys:count: instead", macos(10.0, 10.13), ios(2.0, 11.0), watchos(2.0, 4.0), tvos(9.0, 11.0)); /* These methods are deprecated, and will be marked with API_DEPRECATED in a subsequent release. Use the variants that use errors instead. */ + (nullable NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path API_DEPRECATED_WITH_REPLACEMENT("dictionaryWithContentsOfURL:error:", macos(10.0, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)); + (nullable NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)url API_DEPRECATED_WITH_REPLACEMENT("dictionaryWithContentsOfURL:error:", macos(10.0, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)); - (nullable NSDictionary *)initWithContentsOfFile:(NSString *)path API_DEPRECATED_WITH_REPLACEMENT("initWithContentsOfURL:error:", macos(10.0, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)); - (nullable NSDictionary *)initWithContentsOfURL:(NSURL *)url API_DEPRECATED_WITH_REPLACEMENT("initWithContentsOfURL:error:", macos(10.0, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)); - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile API_DEPRECATED_WITH_REPLACEMENT("writeToURL:error:", macos(10.0, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)); - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically API_DEPRECATED_WITH_REPLACEMENT("writeToURL:error:", macos(10.0, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)); // the atomically flag is ignored if url of a type that cannot be written atomically. @end @interface NSDictionary (NSDictionaryCreation) + (instancetype)dictionary; + (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType )key; + (instancetype)dictionaryWithObjects:(const ObjectType _Nonnull [_Nullable])objects forKeys:(const KeyType _Nonnull [_Nullable])keys count:(NSUInteger)cnt; + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION NS_SWIFT_UNAVAILABLE("Use dictionary literals instead"); + (instancetype)dictionaryWithDictionary:(NSDictionary *)dict; + (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray> *)keys; - (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION; - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary; - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag; - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray> *)keys; /* Reads dictionary stored in NSPropertyList format from the specified url. */ - (nullable NSDictionary *)initWithContentsOfURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)); /* Reads dictionary stored in NSPropertyList format from the specified url. */ + (nullable NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) NS_SWIFT_UNAVAILABLE("Use initializer instead"); @end /**************** Mutable Dictionary ****************/ @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(KeyType)aKey; - (void)setObject:(ObjectType)anObject forKey:(KeyType )aKey; - (instancetype)init NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; @end @interface NSMutableDictionary (NSExtendedMutableDictionary) - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary; - (void)removeAllObjects; - (void)removeObjectsForKeys:(NSArray *)keyArray; - (void)setDictionary:(NSDictionary *)otherDictionary; - (void)setObject:(nullable ObjectType)obj forKeyedSubscript:(KeyType )key API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); @end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems; + (nullable NSMutableDictionary *)dictionaryWithContentsOfFile:(NSString *)path; + (nullable NSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL *)url; - (nullable NSMutableDictionary *)initWithContentsOfFile:(NSString *)path; - (nullable NSMutableDictionary *)initWithContentsOfURL:(NSURL *)url; @end @interface NSDictionary (NSSharedKeySetDictionary) /* Use this method to create a key set to pass to +dictionaryWithSharedKeySet:. The keys are copied from the array and must be copyable. If the array parameter is nil or not an NSArray, an exception is thrown. If the array of keys is empty, an empty key set is returned. The array of keys may contain duplicates, which are ignored (it is undefined which object of each duplicate pair is used). As for any usage of hashing, is recommended that the keys have a well-distributed implementation of -hash, and the hash codes must satisfy the hash/isEqual: invariant. Keys with duplicate hash codes are allowed, but will cause lower performance and increase memory usage. */ + (id)sharedKeySetForKeys:(NSArray> *)keys API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); @end @interface NSMutableDictionary (NSSharedKeySetDictionary) /* Create a mutable dictionary which is optimized for dealing with a known set of keys. Keys that are not in the key set can still be set into the dictionary, but that usage is not optimal. As with any dictionary, the keys must be copyable. If keyset is nil, an exception is thrown. If keyset is not an object returned by +sharedKeySetForKeys:, an exception is thrown. */ + (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keyset API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); @end @interface NSDictionary (NSGenericFastEnumeration) - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(K __unsafe_unretained _Nullable [_Nonnull])buffer count:(NSUInteger)len; @end NS_HEADER_AUDIT_END(nullability, sendability)