Tuesday, May 7, 2013

NSDictionary dictionaryWithObject example ios


dictionaryWithObject :forKey:

Creates and returns a dictionary containing a given key and value.
+ (id)dictionaryWithObject:(id)anObject forKey:(id < NSCopying >)aKey
Parameters of [NSDictionary dictionaryWithObject]
anObject
The value corresponding to aKey.
If this value is nil, an NSInvalidArgumentException is raised.
aKey
The key for anObject.
If this value is nil, an NSInvalidArgumentException is raised.
Return Value
A new dictionary containing a single object, anObject, for a single key, aKey.
Example of [NSDictionary dictionaryWithObject]
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                                                       forKeys:keys];
Example of [NSDictionary dictionaryWithObject]
NSObject * obj = [[NSObject alloc] init];
NSDictionary * dict = [NSDictionary dictionaryWithObject :obj forKey:@"test"];

NSDictionary dictionaryWithDictionary example ios


dictionaryWithDictionary:

Creates and returns a dictionary containing the keys and values from another given dictionary.
+ (id)dictionaryWithDictionary:(NSDictionary *)otherDictionary
Parameters of [NSDictionary dictionaryWithDictionary]
otherDictionary
A dictionary containing the keys and values with which to initialize the new dictionary.
Return Value
A new dictionary containing the keys and values found in otherDictionary.
Example of [NSDictionary dictionaryWithDictionary]
+ (NSMutableDictionary *)safeDictionaryWithDictionary:(NSDictionary *)dictionary
{
    return [NSMutableDictionary dictionaryWithDictionary:(dictionary ? dictionary : @{});
}
Example of [NSDictionary dictionaryWithDictionary]
@interface NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings;
@end

@implementation NSDictionary (JRAdditions)

- (NSDictionary *) dictionaryByReplacingNullsWithStrings {

   const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary :self];
   const id nul = [NSNull null];
   const NSString *blank = @"";

   for(NSString *key in self) {
      const id object = [self objectForKey:key];
      if(object == nul) {
         //pointer comparison is way faster than -isKindOfClass:
         //since [NSNull null] is a singleton, they'll all point to the same
         //location in memory.
         [replaced setObject:blank 
                      forKey:key];
      }
   }
   return [NSDictionary dictionaryWithDictionary :replaced];
}

@end

NSDictionary dictionaryWithContentsOfURL example ios


dictionaryWithContentsOfURL:

Creates and returns a dictionary using the keys and values found in a resource specified by a given URL.
+ (id)dictionaryWithContentsOfURL:(NSURL *)aURL
Parameters
aURL
An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary.
Return Value of [NSDictionary dictionaryWithContentsOfURL]
A new dictionary that contains the dictionary at aURL, or nil if there is an error or if the contents of the resource are an invalid representation of a dictionary.
Discussion of [NSDictionary dictionaryWithContentsOfURL]
The dictionary representation in the file identified by path must contain only property list objects (NSStringNSDataNSDateNSNumberNSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
Example of [NSDictionary dictionaryWithContentsOfURL]
- (void)viewDidLoad {
    ...
    fullData = [NSDictionary dictionaryWithContentsOfURL :url];
    [fullData retain];
    [super viewDidLoad];
}
Example of [NSDictionary dictionaryWithContentsOfURL]
-(void)getData {
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:
@"http://exaple.com/example.plist"]];
NSDictionary *data = [NSDictionary dictionaryWithContentsOfURL :url];
NSString *aKey  = [chamUpdateData objectForKey:@"count"];
}

NSDictionary dictionaryWithContentsOfFile example ios


dictionaryWithContentsOfFile:

Creates and returns a dictionary using the keys and values found in a file specified by a given path.
+ (id)dictionaryWithContentsOfFile:(NSString *)path
Parameters
path
A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary.
Return Value of [NSDictionary dictionaryWithContentsOfFile]
A new dictionary that contains the dictionary at path, or nil if there is a file error or if the contents of the file are an invalid representation of a dictionary.
Discussion of [NSDictionary dictionaryWithContentsOfFile]
The dictionary representation in the file identified by path must contain only property list objects (NSStringNSDataNSDateNSNumberNSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
Example of [NSDictionary dictionaryWithContentsOfFile]
NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile:[ [ [ NSBundle      mainBundle] bundlePath ] stringByAppendingPathComponent:@"MyLevels.plist" ] ];

NSMutableArray *panelNames1;                                                         
panelNames1 = [[NSMutableArray alloc] init];

for (id theKey in mydictionary) {        
    [panelNames1 addObject:[mydictionary objectForKey:theKey]]; 
}
Example of [NSDictionary dictionaryWithContentsOfFile]
NSMutableArray *mutableArray =[[NSMutableArray alloc] initWithObjects:nil];
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"plist"]];

   //--get parent dictionary/Array named animals which holds the animals dictionary items
    NSDictionary *parentDictionary = [mainDictionary objectForKey:@"animals"];


    //---enumerate through the dictionary objects inside the parentDictionary
    NSEnumerator *enumerator = [parentDictionary objectEnumerator];
    id value;

    while ((value = [enumerator nextObject])) {
        if ([[value valueForKey:@"minAge"] isEqualToNumber:[NSNumber numberWithInt:3]]) 
        {            

            [mutableArray addObject:[value valueForKey:@"name"]];                  
        }


    }

NSCache setTotalCostLimit example ios


setTotalCostLimit:

Sets the maximum total cost that the cache can have before it starts evicting objects.
- (void)setTotalCostLimit:(NSUInteger)lim
Parameters of [NSCache setTotalCostLimit]
lim
The maximum total cost that the cache can have before it starts evicting objects.
Example of [NSCache setTotalCostLimit]
cache = [[NSCache alloc] init];
[cache setCountLimit:100];
[cache setTotalCostLimit:1500000];
[cache setEvictsObjectsWithDiscardedContent:YES];

NSCache totalCostLimit example ios


totalCostLimit

Returns the maximum total cost that the cache can have before it starts evicting objects.
- (NSUInteger)totalCostLimit
Return Value
The current maximum cost that the cache can have before it starts evicting objects.
Discussion of [NSCache totalCostLimit]
The default value is 0, which means there is no limit on the size of the cache. If you add an object to the cache, you may pass in a specified cost for the object, such as the size in bytes of the object. If adding this object to the cache causes the cache’s total cost to rise above totalCostLimit, the cache could automatically evict some of its objects until its total cost falls below totalCostLimit. The order in which the cache evicts objects is not guaranteed. This limit is not a strict limit, and if the cache goes over the limit, an object in the cache could be evicted instantly, at a later point in time, or possibly never, all depending on the implementation details of the cache.
Example of [NSCache totalCostLimit]
cache = [[NSCache alloc] init];
[cache setCountLimit:100];
[cache setTotalCostLimit:1500000];
[cache setEvictsObjectsWithDiscardedContent:YES];

NSCache setObject example ios


setObject :forKey:

Sets the value of the specified key in the cache.
- (void)setObject:(id)obj forKey:(id)key
Parameters
obj
The object to be stored in the cache.
key
The key with which to associate the value.
Discussion of [NSCache setObject]
Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.
Example of [NSCache setObject]

id object = [NSString stringWithFormat:@"%lu", theIndex % 10];
id key = [NSString stringWithFormat:@"sdffffffff%lu", theIndex];
[self.cache setObject :object forKey:key];
Example of [NSCache setObject]
// Your cache should have a lifetime beyond the method or handful of methods
// that use it. For example, you could make it a field of your application
// delegate, or of your view controller, or something like that. Up to you.
NSCache *myCache = ...;
NSAssert(myCache != nil, @"cache object is missing");

// Try to get the existing object out of the cache, if it's there.
Widget *myWidget = [myCache objectForKey: @"Important Widget"];
if (!myWidget) {
    // It's not in the cache yet, or has been removed. We have to
    // create it. Presumably, creation is an expensive operation,
    // which is why we cache the results. If creation is cheap, we
    // probably don't need to bother caching it. That's a design
    // decision you'll have to make yourself.
    myWidget = [[[Widget alloc] initExpensively] autorelease];

    // Put it in the cache. It will stay there as long as the OS
    // has room for it. It may be removed at any time, however,
    // at which point we'll have to create it again on next use.
    [myCache setObject: myWidget forKey: @"Important Widget"];
}

// myWidget should exist now either way. Use it here.
if (myWidget) {
    [myWidget runOrWhatever];
}